vaughner Posted October 18, 2009 Share Posted October 18, 2009 (edited) Alright, I have been banging my head against a wall for days with this one and am finally breaking down and asking for help. Now before anyone asks, I HAVE searched the whole forum, up and down, for information on this problem and one of the answers provided me 'some' help, but I am still at a loss... so without further a do, here we go.Post my ideas came from...I have too convert 3x 4 byte hex values that represent floats. These floats represent a point in three dimensional space, x,y,z coordinates.0x8D2B33C6 - X Position in space0x698FC344 - Y Position in space0xC5524242 - Z Position in spaceNow they are currently in network byte order, or Big Endian and need to be converted to Little Endian. So then we have this...0xC6332B8D0x44C38F690x424252C5 - Respectivly... NOTE: The endian converter in the code handles this with the input, so this is just for ease of reading.Now using the following code:#include <String.au3> #Include <WinAPI.au3> $start = "698FC344" $end = hexToFloat($start) MsgBox(0, '', $end) func endian_converter($value) local $byte0 = BitShift($value,24) local $byte1 = BitAND(BitShift($value,-8), 0x00FF0000) local $byte2 = BitAND(BitShift($value,8), 0x0000FF00) local $byte3 = BitShift($value,-24) local $result = BitOR($byte0,$byte1,$byte2,$byte3) return $result endfunc Func hexToFloat($hex) return StringFormat("%f",_WinAPI_IntToFloat(endian_converter(Number("0x" & $hex)))) EndFuncI get "1564.48156738281" which is correct, however when I replace the hex with this "8D2B33C6" i get "-1.#QNAN" (should be -11466.89)About HALF of my computations return this "Quiet Not A Number" and I don't know why. I use another program that does hex/binary/float conversions and they all work out correctly there, why not here?So I guess what I'm asking is why does this not work all the time and why is it returning a QNAN for some of these values? Do I need to create my own HexToFloat32 function? If so, I have run through ALL the wikipedia and google information I can find and still don't understand how to recreate these floats from the hex data.ANY help would be appreciated here!Thanks to anyone who takes the time to read this and especially anyone who responds!~Vaughner Edited October 19, 2009 by vaughner Please click me if you have a minute! Thanks! Link to comment Share on other sites More sharing options...
Authenticity Posted October 18, 2009 Share Posted October 18, 2009 #include <WinAPI.au3> Local $X = 0x8D2B33C6 ; X Position in space Local $Y = 0x698FC344 ; Y Position in space Local $Z = 0xC5524242 ; Z Position in space ConsoleWrite(_WinAPI_IntToFloat('0x' & SwapEndian($X)) & @CRLF) ConsoleWrite(_WinAPI_IntToFloat('0x' & SwapEndian($Y)) & @CRLF) ConsoleWrite(_WinAPI_IntToFloat('0x' & SwapEndian($Z)) & @CRLF) ; Thank trancexx for this baby, http://www.autoitscript.com/forum/index.php?showtopic=103630 Func SwapEndian($iValue) Return Hex(BinaryMid($iValue, 1, 4)) EndFunc Link to comment Share on other sites More sharing options...
vaughner Posted October 19, 2009 Author Share Posted October 19, 2009 #include <WinAPI.au3> Local $X = 0x8D2B33C6 ; X Position in space Local $Y = 0x698FC344 ; Y Position in space Local $Z = 0xC5524242 ; Z Position in space ConsoleWrite(_WinAPI_IntToFloat('0x' & SwapEndian($X)) & @CRLF) ConsoleWrite(_WinAPI_IntToFloat('0x' & SwapEndian($Y)) & @CRLF) ConsoleWrite(_WinAPI_IntToFloat('0x' & SwapEndian($Z)) & @CRLF) ; Thank trancexx for this baby, http://www.autoitscript.com/forum/index.php?showtopic=103630 Func SwapEndian($iValue) Return Hex(BinaryMid($iValue, 1, 4)) EndFunc Changed to this for my needs. (I'm pulling the data out of a database, and it didn't work your way. :/) #include <WinAPI.au3> ; $aRow's come from a SQLite Query of my DBase Local $X = $aRow[0] ; X Position in space Local $Y = $aRow[1] ; Y Position in space Local $Z = $aRow[2] ; Z Position in space $X = Ptr($X) $Y = Ptr($Y) $Z = Ptr($Z) ConsoleWrite(_WinAPI_IntToFloat('0x' & SwapEndian($X)) & @CRLF) ConsoleWrite(_WinAPI_IntToFloat('0x' & SwapEndian($Y)) & @CRLF) ConsoleWrite(_WinAPI_IntToFloat('0x' & SwapEndian($Z)) & @CRLF) ; Thank trancexx for this baby, http://www.autoitscript.com/forum/index.php?showtopic=103630 Func SwapEndian($iValue) Return Hex(BinaryMid($iValue, 1, 4)) EndFunc Seems when you define your value in the program.. IE: Local $X = 0xf3278343 that $X represents the pointer already? But when you pull the value from an external source, it only contains the value. This forces the pointer to be returned and allows the WinAPI_IntToFloat to proceed properly. Thanks Again! Please click me if you have a minute! Thanks! Link to comment Share on other sites More sharing options...
notsure Posted October 19, 2009 Share Posted October 19, 2009 Did you also come by this website? http://babbage.cs.qc.edu/IEEE-754/32bit.html If you look into the source of the website and you're able to translate the source you should be able to build your own conversionthingy. Link to comment Share on other sites More sharing options...
vaughner Posted October 19, 2009 Author Share Posted October 19, 2009 Thanks notsure, I have just saved a copy of the source of that page for future reference. I hope this topic makes it clear for anyone needing hexToFloat conversions in the future. I even tried to title it so others can search it easily! Personally, I'm turning it into a UDF so I don't have to worry about it again! Please click me if you have a minute! Thanks! Link to comment Share on other sites More sharing options...
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