Jump to content

Hex conversions


Go to solution Solved by Nine,

Recommended Posts

To start, my recollection of Hex is bare bones. As in just about only recalling how to convert dec to hex type of bare bones.

The problem:

I have two things that are supposed to be equal. Namely a .txt file and a blob of Hex values in a database. 

I have documentation for which bytes are supposed to represent which columns in the txt file.

The inputs

  1. $HexTime = "036fa5ba"
  2. $HexLong = "c057119da597d49d"
  3. $HexRSL = "c288c7ae"

 

  1. $Time = 57648570
  2. $Long = -92.275247
  3. $RSL = -68.39

Goal:

I need to be able to go both ways. 

What I have:
Best I can tell everything is in "big endian".

$HexTime = "036fa5ba"
$HexLong = "0xc057119da597d49d"
$HexRSL = "0xc288c7ae"

$Time=57648570
$Long=-92.275247
$RSL=-68.39

ConsoleWrite("Time:" & Dec(($HexTime)) & @CRLF)
ConsoleWrite("lng:" & _WinAPI_IntToFloat(($HexLong)) & @CRLF)
ConsoleWrite("RSL:" & _WinAPI_IntToFloat(($HexRSL)) & @CRLF)

ConsoleWrite("Time:" & Hex(($Time)) & @CRLF)
ConsoleWrite("lng:" & Hex(_WinAPI_FloatToInt(($Long))) & @CRLF)
ConsoleWrite("RSL:" & Hex(_WinAPI_FloatToInt(($RSL))) & @CRLF)

The 4 byte time and RSL work just fine. However, the ones for the longitude doesn't work, presumably because it is 8 byte or double, not float.

Time:57648570
lng:-2.63383968506439e-16
RSL:-68.3899993896484
Time:036FA5BA
lng:C2B88CED
RSL:C288C7AE

I have tried various other things, like this:

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

$HexLong = "c057119da597d49d"
$byte1 = StringMid($HexLong,1,2)
$byte2 = StringMid($HexLong,3,2)
$byte3 = StringMid($HexLong,5,2)
$byte4 = StringMid($HexLong,7,2)
$byte5 = StringMid($HexLong,9,2)
$byte6 = StringMid($HexLong,11,2)
$byte7 = StringMid($HexLong,13,2)
$byte8 = StringMid($HexLong,15,2)

; Put the raw byte data into the source
DllStructSetData($src, 1, $byte1)
DllStructSetData($src, 2, $byte2)
DllStructSetData($src, 3, $byte3)
DllStructSetData($src, 4, $byte4)
DllStructSetData($src, 5, $byte5)
DllStructSetData($src, 6, $byte6)
DllStructSetData($src, 7, $byte7)
DllStructSetData($src, 8, $byte8)

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

But it outputs "2.53979544568635e-265" for this version. I've tried other versions but can't get it to work. 

This was adapted from

 

Please ignore the ugly hardcode. I'll convert it to a for loop if it ends up that route.

Link to comment
Share on other sites

  • Solution
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...