The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Jackal on April 13, 2010, 05:19:22 PM

Title: Byte array to Int
Post by: Jackal on April 13, 2010, 05:19:22 PM
I was wondering how do I convert a byte array to an integer.

for instance 00 00 2B 6F     = 16,375
Title: Re: Byte array to Int
Post by: redskull on April 13, 2010, 05:30:47 PM
The easiest way is BSWAP.  However, there are all sorts of various ways using logic, rotating, shifting, and multiplying, and you will more than likely get 100 different responses as the members fight to the death over which is fastest.

-r
Title: Re: Byte array to Int
Post by: Ghandi on April 13, 2010, 06:11:04 PM
Quote
and you will more than likely get 100 different responses as the members fight to the death over which is fastest.

rofl :D

HR,
Ghandi
Title: Re: Byte array to Int
Post by: dedndave on April 13, 2010, 06:15:25 PM
actually, speed for that is only critical if you have to convert thousands of values
usually, these routines are used a few times - go for something small and convenient to use
you can use some of the C lib functions if you want
Title: Re: Byte array to Int
Post by: Slugsnack on April 13, 2010, 08:55:01 PM
Quote from: Jackal on April 13, 2010, 05:19:22 PM
I was wondering how do I convert a byte array to an integer.

for instance 00 00 2B 6F     = 16,375
i maybe missing something but how is that even a conversion. i'd have thought it might convert to 11119 or 1865089024. where did 16375 come from :|
Title: Re: Byte array to Int
Post by: dedndave on April 13, 2010, 09:03:23 PM
...or 0,0,43,111
he did say it was a byte array   :bg
but, you're right, Mike
the answer he probably wants is 1865089024
Title: Re: Byte array to Int
Post by: Jackal on April 13, 2010, 10:07:33 PM
Quote from: dedndave on April 13, 2010, 09:03:23 PM
...or 0,0,43,111
he did say it was a byte array   :bg
but, you're right, Mike
the answer he probably wants is 1865089024



That answer is wrong. The answer was wrong in my first post also. I got it i think. Its Big Endian and Int32 so my asnwer I needed to get was 11,119.
Title: Re: Byte array to Int
Post by: qWord on April 13, 2010, 10:20:31 PM
Quote from: Jackal on April 13, 2010, 10:07:33 PMIts Big Endian and Int32 so my asnwer I needed to get was 11,119.
load the value into an register and use bswap r32 to convert it from network byte order to littel endian. Then yo can use the sdword$(r32/mem32)-macro to convert it to an string (or use directly crt_sprintf-function)
mov eax,SDWORD ptr ...
bswap eax
invoke MessageBoxA,0,sdword$(eax),0,0
Title: Re: Byte array to Int
Post by: jj2007 on April 13, 2010, 10:57:06 PM
Quote from: qWord on April 13, 2010, 10:20:31 PM
bswap eax,eax

That is a cute addition to the x86 instruction set, thanks. I hope bswap eax, [ebx] works, too :green
Title: Re: Byte array to Int
Post by: qWord on April 13, 2010, 10:59:22 PM
Quote from: jj2007 on April 13, 2010, 10:57:06 PM
That is a cute addition to the x86 instruction set, thanks. I hope bswap eax, [ebx] works, too :green
:bg
Title: Re: Byte array to Int
Post by: Jackal on April 14, 2010, 03:54:26 AM
all taken care of. Thanks.