ACS Posted March 4, 2009 Posted March 4, 2009 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?
Richard Robertson Posted March 4, 2009 Posted March 4, 2009 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.
ACS Posted March 4, 2009 Author Posted March 4, 2009 (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 March 4, 2009 by ACS
Richard Robertson Posted March 4, 2009 Posted March 4, 2009 Hmm. Try making another struct with a byte array. Then you can set your data into that and get that struct pointer.
ACS Posted March 4, 2009 Author Posted March 4, 2009 HOLY CRAP IT WORKED! 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. toto22 1
Richard Robertson Posted March 4, 2009 Posted March 4, 2009 I would have done a byte array, but four separate bytes works too.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now