Sign in to follow this  
Followers 0
ACS

Converting to float and double

6 posts in this topic

Hey all, is there a function similar to Dec() which will return a numeric representation of a hexadecimal string? I'm reading data from a binary file, and I can read shorts and ints no problem, but there doesn't seem to be a function that will take a hex string and "convert" it to a float or double data format.

Does anyone know of a way to do this, short of performing a manual conversion by operating directly on the bytes?

Share this post


Link to post
Share on other sites



The problem with floating point numbers is the way the data is stored. If you were to perform a direct byte copy of memory into a floating point number, that would work. I don't know of any other way though.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Interesting idea, thanks Richard.

I presume I'll have to use the _MemMoveMemory UDF but I'm not sure what to pass as the first parameter (AFAIK AutoIt can't work directly with variable pointers.)

I have something like:

$var = Binary("0x0134624A")
$f = DllStructCreate("float")
$ptr = DllStructGetPtr($f)
_MemMoveMemory(????, $ptr, 4)

But what goes in place of the question marks? I can't get a pointer to $var... can I?

I should mention that using Ptr($var) just causes an exception.

Edited by ACS

Share this post


Link to post
Share on other sites

Hmm. Try making another struct with a byte array. Then you can set your data into that and get that struct pointer.

Share this post


Link to post
Share on other sites

HOLY CRAP IT WORKED! :P:unsure::D

Thank you so much, this is exactly what I need!

For those interested, here's how I did it:

; Set up the source and destination structures
$src = DllStructCreate("byte;byte;byte;byte")
$psrc = DllStructGetPtr($src)
$dest = DllStructCreate("float")
$pdest = DllStructGetPtr($dest)

; Put the raw byte data into the source
DllStructSetData($src, 1, $byte1)
DllStructSetData($src, 2, $byte2)
DllStructSetData($src, 3, $byte3)
DllStructSetData($src, 4, $byte4)

; Perform the "conversion"
_MemMoveMemory($psrc, $pdest, 4)
ConsoleWrite(DllStructGetData($dest, 1) & @CRLF)

Where $byte1 through $byte4 are the byte values IN LITTLE ENDIAN ORDER.

Share this post


Link to post
Share on other sites

I would have done a byte array, but four separate bytes works too.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0