Hey folks,

I have a variable I created with HeapAlloc. And the pointer to it is ptrSections (m32). The variable is 16 bytes long, and I want to get a number out of bytes 5-8.
Let's say those bytes were "abcd". I need to convert that into a number like this:
Number = (ascii of a) + [(ascii of b) * 256] + [(ascii of b) * 65536] + [(ascii of b) * 16777216]
So how do I do that? I just have the 16 bytes variable and the pointer to it, I tried it serveral ways, but couldn't figure it out.. so please help me!


Posted on 2003-11-12 20:30:34 by yaXay
What you are describing looks like the ascii representation of the value of a DWORD. Using MASM syntax, you can get the binary value of that DWORD as follows:
mov ebx,ptrSections ;copy the pointer to EBX

mov eax,[ebx+4] ;get the DWORD starting at the 5th byte into EAX
At this point, there are numerous ways to convert that DWORD to ascii. One of them is to invoke the dwtoa function of the M32lib library of the MASM32 package (which has a flaw in it), or the dw2a function of the same package which simply calls upon the Windows wsprintf function.

Or you can write your own procedure. A simple one could be as follows for an unsigned integer, assuming EAX contains the DWORD:
   push edi

push 0
mov ecx,10
xor edx,edx ;prepare for division
div ecx
add dl,30h ;convert to an ascii character
push edx ;store it on the stack
or eax,eax
jnz @B

mov edi,offset buffer
pop eax ;retrieve the results one by one
or eax,eax ;until no more
jz @F
stosb ;copy it to the buffer
jmp @B

stosb ;terminate string with a 0
pop edi ;restore EDI
Posted on 2003-11-12 22:26:30 by Raymond
I think I didn't make it clear enough. It's not a decimal string. When I said ascii of a I ment the ascii code. So the ascii code of '0' would be 48d/30h!
I know my string is 4 bytes long, my proc looks like this:

asctodw proc uses esi ptrString:PTR BYTE

mov eax,0d ' clear aex
mov esi,[ptrString] ' move string pointer to esi
mov al,[esi] ' get the first byte
shl eax,8d ' shift 8bits to the left
inc esi ' point to next byte
mov al,[esi] ' get next byte
shl eax,8d ' and so on..
inc esi
mov al,[esi]
shl eax,8d
inc esi
mov al,[esi]
shl eax,8d

ret ' return
asctodw endp

I'm gonna check it when I'm back at home, but correct me if you already found any mistakes..

Posted on 2003-11-13 14:47:02 by yaXay
This should do it for you, since the values you have in there are ascii and you do not wish to convert them to numeric values just mov them into a dword as is. If you wish to convert them to thier hex equivalent that's another story.

mov eax,ptrSections

add eax,4 ;offset to the fifth byte
mov eax,[eax]

If you need to reverse the order use bswap eax
Posted on 2003-11-13 15:12:59 by donkey
Hey, I found my mistake..
I had a wrong parameter..
I invoked ReadFle like this:
ReadFile,hFile,,8d,ADDR ddBytesRead,NULL
It had to be like this:
ReadFile,hFile,ebx,8d,ADDR ddBytesRead,NULL

Since I didn't erase the memory I had some random data, which of course didn't help much..
Thanks anyway..

Posted on 2003-11-13 16:25:27 by yaXay

I didn't see anything about ReadFile in there at all, maybe my old eyes are starting to go :)
Posted on 2003-11-13 16:27:50 by donkey
That last shl eax,8d in your posted asctodw proc may also cause you a lot of grief. Before that instruction, the EAX content would be something like ABCD which would be what you are looking for. But, by doing that last shift, EAX would become BCD0! :(

Posted on 2003-11-13 23:14:18 by Raymond