Jump to content

Converting to float and double


 Share

Recommended Posts

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?

Link to comment
Share on other sites

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
Link to comment
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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...