AutoHotkey Community

It is currently May 27th, 2012, 5:43 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: March 16th, 2010, 10:21 pm 
Offline

Joined: March 16th, 2010, 9:08 pm
Posts: 11
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]:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2010, 2:41 am 
Offline
User avatar

Joined: October 7th, 2006, 8:45 am
Posts: 3330
Location: Simi Valley, CA
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!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2010, 4:07 am 
Offline

Joined: March 16th, 2010, 9:08 pm
Posts: 11
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"?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2010, 4:28 am 
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")


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2010, 4:43 am 
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)


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2010, 4:43 am 
Offline

Joined: March 16th, 2010, 9:08 pm
Posts: 11
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?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2010, 5:14 am 
Offline

Joined: March 16th, 2010, 9:08 pm
Posts: 11
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)?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 17th, 2010, 5:52 am 
Offline

Joined: March 16th, 2010, 9:08 pm
Posts: 11
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.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Apollo, Google Feedfetcher, mrhobbeys, rbrtryn and 60 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group