Hex String to Double
-
Hi all,
I'm trying to decode some files.
One of the values I need is stored as a double in hex.
Example - "3fb999999999999a" -> "0.1".
The double conversion here gets the correct result: https://gregstoll.dyndns.org/~gregstoll/floattohex/
Currently my code just has the hex stored as a string (as in "set hexstr 3fb999999999999a"). How do I replicate the conversion done on that site?
(TCL)
-
@ben_warre Uh, it links to the source code right there on the page:
https://gregstoll.dyndns.org/~gregstoll/floattohex/src/App.js
What specifically are you having trouble with? Can't you just transcode his JS into whatever language you're using?
-
@blakeyrat having difficulty decoding it... and matching functions.
(plus it's easier to ask the experts)
-
Put together after staring at https://www.tcl.tk/man/tcl8.4/TclCmd/binary.htm for some time:
$ perl -E'say unpack "H*", pack "d", 0.1' # this makes hex string representing a double in native format 9a9999999999b93f $ tclsh % binary scan [ binary format H* 9a9999999999b93f ] d dvar 1 % puts $dvar 0.1
You can see that this depends on the processor architecture (I had to provide the string in little endian for it to work), so if you go this way, you'll have to swap the hex characters first.
-
Here's the Go version just in case you need another version in the wrong language:
-
@aitap I knew there must be a way of doing it like that somehow!
-
In Java:
double d = Double.longBitsToDouble(Long.parseLong("3fb999999999999a", 16));
In Rust:
let d = f64::from_bits(u64::from_str_radix("3fb999999999999a", 16).unwrap())
-
I actually posted a Javascript version of this a week and a half ago.
https://what.thedailywtf.com/post/1368987 (/topic/25325/call-order-verification/25)
@aitap said in Hex String to Double:
You can see that this depends on the processor architecture (I had to provide the string in little endian for it to work), so if you go this way, you'll have to swap the hex characters first.
Your code is very nearly the same as "AM" posted here:
Except they gave the hex as an integer
0x...
, and had this to add:If the bytes are given in the same order as the machine expects, then use capital "I" in the [binary format]'s format. If the machine uses the opposite byte order, use a lowercase "i" instead.
-
@aitap said in Hex String to Double:
You can see that this depends on the processor architecture
It also depends on which conversions are supported. With Tcl 8.6, the
Q
conversion is available (double precision float, in guaranteed big-endian form) and that's perfect for this job:% set hexstr 3fb999999999999a 3fb999999999999a % binary scan [binary format H* $hexstr] Q x 1 % puts $x 0.1
Turning that into a form that is usable in production (as opposed to being something I cut-n-paste from an interactive session) is left as an exercise.
-
I agree with whatever @dkf said about TCL.
-
I'm a little curious whether the "dkf" mentioned in the link I posted is our @dkf...
-
@anotherusername I think you know the answer to that. ;)
-
@anotherusername we had that discussion before
-
@anotherusername It's not that common a set of initials…