AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

v1.0.46 released: SubStr() and more expression operators
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Announcements
View previous topic :: View next topic  
Author Message
Laszlo



Joined: 14 Feb 2005
Posts: 3877
Location: Pittsburgh

PostPosted: Wed Jun 13, 2007 8:01 pm    Post subject: Reply with quote

NumGet/Put has to perform data conversion. If you read/write floats, or signed/unsigned numbers different actions take place. It is not only the size. If you specify a size of 7 bytes, what does it mean? Do you want the number in NumGet/Put padded from left or right, especially if it is negative? What if it does not fit? What if the number has a decimal part? There are too many options to be considered, which do not fit to these functions.

I think, what foom has in mind is mem-move. It could be a separate function, with the first parameter a string of hex digits: MemPut("1f2233",var,offs,3) or String := MemGet(var,0,33). Or name them HexGet/HexPut. They can be useful at:
- filling constant parts of large structures with one instruction
- getting the content of binary files into an AHK variable in manageable form
- creating dynamic variable names from arbitrary data (~associative arrays), etc.

Btw, Chris: what does NumPut do if the number parameter is too large integer, like NumPut(256,var,0,"char"). The simplest was to ignore the high order bits, which do not fit, but it ought to be documented.
Back to top
View user's profile Send private message Visit poster's website
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10450

PostPosted: Wed Jun 13, 2007 8:32 pm    Post subject: Reply with quote

Laszlo wrote:
I think, what foom has in mind is mem-move. It could be a separate function, with the first parameter a string of hex digits: MemPut("1f2233",var,offs,3) or String := MemGet(var,0,33). Or name them HexGet/HexPut. They can be useful at: ...
Good ideas for the future, but it might be a long time. In the meantime, hopefully DllCall can fill in most of the gaps.

Laszlo wrote:
Btw, Chris: what does NumPut do if the number parameter is too large integer, like NumPut(256,var,0,"char"). The simplest was to ignore the high order bits, which do not fit, but it ought to be documented.
For both signed and unsigned, it converts the value to an int64 then casts it to the unsigned smaller size (never signed because it doesn't seem to matter); e.g. *(unsigned short *)target = (unsigned short)int64_to_write

In this example, I think this means it ignores the high-order 6 bytes and copies the low-order two bytes into the target position. It probably gives no special treatment to whether the number is positive or negative.

This might not be worthwhile to document unless it can be explained concisely and clearly.
Back to top
View user's profile Send private message Send e-mail
Laszlo



Joined: 14 Feb 2005
Posts: 3877
Location: Pittsburgh

PostPosted: Wed Jun 13, 2007 9:03 pm    Post subject: Reply with quote

Proposed documentation extensions for NumPut: If float or double Type is specified, Number is rounded to the supported precision, otherwise its most significant bits are ignored, if any, which do not fit to the specified Type. E.g. NumPut(257,var,0,"Char") results in writing a single byte = 1 to the memory reserved for var.
Back to top
View user's profile Send private message Visit poster's website
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10450

PostPosted: Wed Jun 13, 2007 9:47 pm    Post subject: Reply with quote

I omitted the bit about floats/doubles because it's explained in DllCall types (which is already mentioned and linked). I added a slightly abbreviated version of the rest: "If an integer is too large to fit in the specified Type, its most significant bytes are ignored; e.g. NumPut(257, var, 0, "Char") would store the number 1."

Thanks.
Back to top
View user's profile Send private message Send e-mail
Laszlo



Joined: 14 Feb 2005
Posts: 3877
Location: Pittsburgh

PostPosted: Wed Jun 13, 2007 10:09 pm    Post subject: Reply with quote

HexGet/Put: I don't know fast dll calls for this. NumGet/Put are normally called for each byte in a loop, which is slow. We cannot easily lump 4 bytes together, because the little endian architecture in PC's makes them appear in memory in the wrong order, so an extra dll call is needed: "ntdll\RtlUlongByteSwap". Int64 is even worse, because of the signed hex representation.

Does anyone know a faster way to read/write hex streams to memory? In AHK the functions look like this
Code:
HexGet(addr,len) {
   Static S = 12
   Loop %len%
      DllCall("msvcrt\sprintf", Str,S, Str,"%02X", Char,NumGet(addr-1,A_Index,"UChar")), R .= S
   Return R
}

HexPut(hex,addr) {
   Loop % StrLen(hex)//2
      NumPut("0x" . SubStr(hex,2*A_Index-1,2), addr-1, A_Index, "Char")
}

VarSetCapacity(x,16)
HexPut("00112233445566778899AABBCCDDEEFF",&x)
MsgBox % HexGet(&x,16)
Back to top
View user's profile Send private message Visit poster's website
majkinetor



Joined: 24 May 2006
Posts: 3544
Location: Belgrade

PostPosted: Sat Jun 16, 2007 12:46 pm    Post subject: Reply with quote

Yes, that is very useful, I needed that many times.
_________________
Back to top
View user's profile Send private message MSN Messenger
corrupt



Joined: 29 Dec 2004
Posts: 2328

PostPosted: Sun Jun 17, 2007 5:45 am    Post subject: Reply with quote

Which page was the release and documentation on again?... Rolling Eyes

Laszlo wrote:
Does anyone know a faster way to read/write hex streams to memory?
Ask For Help section (or dev?)...??
Back to top
View user's profile Send private message Visit poster's website
engunneer



Joined: 30 Aug 2005
Posts: 5957
Location: Pacific Northwest, US

PostPosted: Sun Jun 17, 2007 5:57 am    Post subject: Reply with quote

page 6 of this topic, unless I completely misunderstand what you are asking (very likely)
_________________
Unless otherwise noted, all code is untested.
Common Answers: 1.(Loops, Viruses, etc.) 2. Search 3.RTFM
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Announcements All times are GMT
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8
Page 8 of 8

 
Jump to:  
You cannot post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group