 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Predatech
Joined: 16 Mar 2010 Posts: 11
|
Posted: Tue Mar 16, 2010 9:21 pm Post subject: NumGet() and NumPut() Custom types |
|
|
I am trying to store a collection of 3-byte values into a variable and then read back certain ones based on the Offset value.
For the Type value, I can get "Char" to return 1 byte, and "Short" to return 2 bytes, everything else seems to return 4 bytes.
I would like to get 3 bytes back from the specified offset, but it doesn't seem to work if I specify the Type as 3 (or 2, or 4). Only the quoted literal variable types work.
Am I missing something?
| Code: |
SetFormat, IntegerFast, hex
VarSetCapacity (Myvar,20,0)
MyVar = FFEEDDCCBBAA
A := numget(MyVar,0, 2)
B := numget(MyVar,3, "CHAR")
C := numget(MyVar,4, "Short")
D := numget(MyVar,8, 3)
msgbox, %MyVar%
E := A * 2
ListVars
msgbox %E%
|
Global Variables (alphabetical)
--------------------------------------------------
0[3 of 3]: 0x0
A[10 of 63]: 0x45454646
B[4 of 7]: 0x45
C[6 of 7]: 0x4444
D[10 of 63]: 0x41414242
E[10 of 63]: 0x8a8a8c8c
ErrorLevel[1 of 3]: 0
Myvar[12 of 63]: FFEEDDCCBBAA
VarSetCapacity[0 of 0]:
|
|
| Back to top |
|
 |
[VxE]
Joined: 07 Oct 2006 Posts: 3254 Location: Simi Valley, CA
|
Posted: Wed Mar 17, 2010 1:41 am Post subject: |
|
|
Firstly, I hope you're not trying to insert hex data when you do MyVar = FFEEDDCCBBAA, because that's the wrong way. When you assign a string to a variable, you are inserting an array of ascii-codes (each of which fills one byte).
Consider this code:
| Code: | SetFormat, IntegerFast, hex
VarSetCapacity (Myvar,20,0)
MyVar = FFEEDDCCBBAA
; if MyVar is an array of 3-byte members, then this is the hex data in MyVar:
; offset hex value
; 0 0x464645
; 3 0x454444
; 6 0x434342
; 9 0x424141
A := numget(MyVar,0, "UInt") & 0xFFFFFF ; using & 0xFFFFFF truncates the highest-
B := numget(MyVar,3, "UInt") & 0xFFFFFF ; order byte in the 4-byte 'UInt'
C := numget(MyVar,6, "UInt") & 0xFFFFFF
D := numget(MyVar,9, "UInt") & 0xFFFFFF
msgbox, %MyVar%
E := A * 2
ListVars
msgbox %E% |
Notice that, although the variables appear to receive the expected bytes, their order is reversed. This is because the 'UInt' data type, which has a size of 4 bytes, uses the leftmost byte as its low order byte.
So, you're left with two options to retrieve a 3-byte struct from a variable: either grab 4 bytes, truncate the last one, then swap bytes 1 and 3, OR take the sum of 3 sequential bytes bitshifted to match their order ( Ch1 << 16 | Ch2 << 8 | Ch3 << 0 ). _________________ Ternary (a ? b : c) guide TSV Table Manipulation Library
Post code inside [code][/code] tags! |
|
| Back to top |
|
 |
Predatech
Joined: 16 Mar 2010 Posts: 11
|
Posted: Wed Mar 17, 2010 3:07 am Post subject: |
|
|
I was just using MyVar = FFEEDDCCBBAA to load some data for testing the numget(). I want to use numput() to load the data, but I had limited success with that also.
Thanks for your input on the numget() it explains a lot.
I guess I have a part 2 question for the numput(). What am I doing wrong here?
| Code: |
SetFormat, IntegerFast, hex
VarSetCapacity (Myvar,20,0)
val1 = 4605509 ; same as 0x464645
val2 = 0x454444
val3 = 0x434342
val4 = 0x424141
numput(val1, MyVar, 0, "UInt")
numput(val2, MyVar, 3, "UInt")
numput(val3, MyVar, 6, "UInt")
numput(val4, MyVar, 9, "UInt")
; if MyVar is an array of 3-byte members, then this is the hex data in MyVar:
; offset hex value
; 0 0x464645
; 3 0x454444
; 6 0x434342
; 9 0x424141
A := numget(MyVar,0, "UInt") & 0xFFFFFF ; using & 0xFFFFFF truncates the highest-
B := numget(MyVar,3, "UInt") & 0xFFFFFF ; order byte in the 4-byte 'UInt'
C := numget(MyVar,6, "UInt") & 0xFFFFFF
D := numget(MyVar,9, "UInt") & 0xFFFFFF
msgbox, %MyVar%
E := A * 2
ListVars
msgbox %E%
|
And also, Does this mean that I must use a quoted named type? it seemed the documentation for these functions said that you could also use "the number of bytes in your type"? |
|
| Back to top |
|
 |
Guest
|
Posted: Wed Mar 17, 2010 3:28 am Post subject: |
|
|
You need to use the "&" operator on myvar in the NumPut statements.
| Code: | SetFormat, IntegerFast, hex
VarSetCapacity (Myvar,20,0)
val1 = 4605509 ; same as 0x464645
val2 = 0x454444
val3 = 0x434342
val4 = 0x424141
numput(val1, &MyVar, 0, "UInt")
numput(val2, &MyVar, 3, "UInt")
numput(val3, &MyVar, 6, "UInt")
numput(val4, &MyVar, 9, "UInt")
|
|
|
| Back to top |
|
 |
Guest
|
Posted: Wed Mar 17, 2010 3:43 am Post subject: |
|
|
btw, there is a really useful memory dump function that can be useful in examining ahk variables in memory. dumpDwords
And you would use it like this with your code:
| Code: | SetFormat, IntegerFast, hex
VarSetCapacity (Myvar,20,0)
val1 = 4605509 ; same as 0x464645
val2 = 0x454444
val3 = 0x434342
val4 = 0x424141
numput(val1, &MyVar, 0, "UInt")
numput(val2, &MyVar, 3, "UInt")
numput(val3, &MyVar, 6, "UInt")
numput(val4, &MyVar, 9, "UInt")
msgbox, % dumpDwords(myvar, 16, 1) |
|
|
| Back to top |
|
 |
Predatech
Joined: 16 Mar 2010 Posts: 11
|
Posted: Wed Mar 17, 2010 3:43 am Post subject: |
|
|
I did try that, but the output of listvars was as below:
Global Variables (alphabetical)
--------------------------------------------------
0[3 of 3]: 0x0
A[0 of 0]: EFFD¬$?
B[0 of 0]: EFFD¬$?
C[0 of 0]: EFFD¬$?
D[0 of 0]: EFFD¬$?
E[0 of 0]: EFFD¬$?
ErrorLevel[1 of 3]: 0
Myvar[0 of 0]: EFFD¬$?
val1[8 of 63]: 0x464645
val2[8 of 63]: 0x454444
val3[8 of 63]: 0x434342
val4[8 of 63]: 0x424141
VarSetCapacity[0 of 0]: EFFD¬$?
I realize that this may be the interpretation of the ASCII character values, but they are all the same. Where does the EFFD¬$? come from? |
|
| Back to top |
|
 |
Predatech
Joined: 16 Mar 2010 Posts: 11
|
Posted: Wed Mar 17, 2010 4:14 am Post subject: |
|
|
Thanks for the DumpDwords function. It explains the odd ASCII characters I've seen.
The second dword starts with a value after the varsetcapacity() command, even though I initialized all to 0. The initial value in the second dword is not overwritten by the numput() commands. This value changes on different runs of the test script. Here is an example:
45 46 46 44 |70 30 3F 00 |43 41 41 42
What is the 70 30 3F 00? Why is it there after the varsetcapacity(MyVar, 20, 0)? |
|
| Back to top |
|
 |
Predatech
Joined: 16 Mar 2010 Posts: 11
|
Posted: Wed Mar 17, 2010 4:52 am Post subject: |
|
|
Since this didn't make sense, having the extra Hex values in the dword, I started a fresh test script using the DumpDwords function to see what was going on.
I realized that the original script had a space between the varsetcapacity and the (). It was:
varsetcapacity ()
not
varsetcapacity()
I guess this led to it not executing the varsetcapacity() command. (Which I discovered by using the DumpDwords function)
Thanks for everyone's help. I have a much better understanding of numput() and numget() now. |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|