Any help would be very, very much appreciated...
I've been searching the net (google) for 4 days now trying to find a php
function to convert hex to floating point.
I'm converting old transactional data files to mysql and I'm stumped trying
to convert the currency data bytes.
For example: I'm looking for a php function to convert (40 3E 51 EB 85 1E
B8 52) hex to ($30.32) foating (double precision 64 bits)
I'm not sure if this convertion is OS dependent??
I'm using php and apache on WinXP (my laptop). But will be migrating to
code a Linux and apache server when complete.
David
================
The following code was posted by Chung Leong. This works very well from
floating point to hex, however, I require a php function to convert hex to
double precision floating point.
=================================
Reverse engineering a file format is not easy. Basically, there's two ways
the records could be stored: fixed-size or variable size. With a hex editor,
try to figure out the size of different records. Variable size records
usually store its length at the first byte or word.
It looks like some of the data is stored as floating points. Here's a couple
function that show the binary representation of single and double precision
floats:
function ieee_double($f) {
$f = (double) $f;
$b = pack("d", $f);
$hex = "";
for($i = 0; $i < strlen($b); $i++) {
$c = ord($b{$i});
$hex .= sprintf(" %02X", $c);
} return $hex;
}
function ieee_float($f) {
$f = (float) $f;
$b = pack("f", $f);
$hex = "";
for($i = 0; $i < strlen($b); $i++) {
$c = ord($b{$i});
$hex .= sprintf(" %02X", $c);
} return $hex;
}