472,810 Members | 3,970 Online

# Hex to floating point

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;
}

Jul 17 '05 #1
5 9676
DvGrimm wrote:
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??
Probably (definitely?) it is.
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.
=================================
I tool your example ("40 3E 51 EB 85 1E B8 52") as a string
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;
}

First you have to reverse the for() loop;
then the pack() call.

I did it like this
<?php
function hexreverse(\$x) {
\$r = '';
\$y = explode(' ', \$x);
foreach (\$y as \$z) {
\$r = \$z . ' ' . \$r;
}
return substr(\$r, 0, -1);
}

function hexify(\$x) {
static \$v = array(
'0'=>0, '1'=>1, '2'=>2, '3'=>3,
'4'=>4, '5'=>5, '6'=>6, '7'=>7,
'8'=>8, '9'=>9, 'A'=>10, 'B'=>11,
'C'=>12, 'D'=>13, 'E'=>14, 'F'=>15,
'a'=>10, 'b'=>11, 'c'=>12, 'd'=>13, 'e'=>14, 'f'=>15,
);
\$r = '';
\$y = explode(' ', \$x);
foreach (\$y as \$z) {
if (!ctype_xdigit(\$z)) return false;
\$tmp = \$v[\$z{0}] * 16 + \$v[\$z{1}];
\$r .= chr(\$tmp);
}
return \$r;
}
\$x0 = "40 3E 51 EB 85 1E B8 52";
\$x1 = hexreverse(\$x0); /* I need this: OS = Linux */
\$x2 = hexify(\$x1); /* reverse the for() loop; */
if (\$x2 === false) die("Invalid input\n");
\$x3 = unpack("d", \$x2); /* reverse the pack() call */
\$y = \$x3[1];
var_dump(\$y); /* dump() the final value. */
?>

--
== ** ## !! !! ## ** ==
bypass the spam filter. I will answer all pertinent mails from a valid address.
Jul 17 '05 #2
DvGrimm wrote:
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.
The unpack function can do this, provided it's in the right format.
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??
It's unlikely to be OS-dependent, but it is certainly machine-dependent.
A little-endian CPU (such as x86) will use the bytes in the opposite
order from a big-endian CPU (such as SPARC or PowerPC). It looks like
your data is in big-endian order, so on a PowerPC-based Macintosh this
simple code works and prints out "30.32":

function bin2double(\$bin) {
\$unpacked = unpack("d", \$bin);
return \$unpacked[1];
}

\$x = "\x40\x3E\x51\xEB\x85\x1E\xB8\x52";
echo bin2double(\$x), "\n";

But on an x86-based Linux PC it produces "3.07073382227E+90". A Windows
PC will probably produce the same result.
I'm using php and apache on WinXP (my laptop). But will be migrating
to code a Linux and apache server when complete.

See the online documentation for the 'unpack' function. There's a
comment on that page with some code for swapping the byte order in the
easily. (Byte-swapping is the same operation in either direction, so you
should just need to change the condition that triggers the swap.)

http://www.php.net/unpack

-- brion vibber (brion @ pobox.com)
Jul 17 '05 #3
Why write your own hex decoder when there's good old sscanf()? A more
compact version:

\$s = "40 3E 51 EB 85 1E B8 52";
\$hex = sscanf(\$s, "%02x %02x %02x %02x %02x %02x %02x %02x");
\$hex = array_reverse(\$hex);
\$bin = implode('', array_map('chr', \$hex));
\$array = unpack("dnum", \$bin);
echo \$array['num'];

"Pedro Graca" <he****@dodgeit.com> wrote in message
news:sl*******************@ID-203069.user.uni-berlin.de...
DvGrimm wrote:
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??
Probably (definitely?) it is.
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.
=================================

I tool your example ("40 3E 51 EB 85 1E B8 52") as a string
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;
}

First you have to reverse the for() loop;
then the pack() call.

I did it like this
<?php
function hexreverse(\$x) {
\$r = '';
\$y = explode(' ', \$x);
foreach (\$y as \$z) {
\$r = \$z . ' ' . \$r;
}
return substr(\$r, 0, -1);
}

function hexify(\$x) {
static \$v = array(
'0'=>0, '1'=>1, '2'=>2, '3'=>3,
'4'=>4, '5'=>5, '6'=>6, '7'=>7,
'8'=>8, '9'=>9, 'A'=>10, 'B'=>11,
'C'=>12, 'D'=>13, 'E'=>14, 'F'=>15,
'a'=>10, 'b'=>11, 'c'=>12, 'd'=>13, 'e'=>14, 'f'=>15,
);
\$r = '';
\$y = explode(' ', \$x);
foreach (\$y as \$z) {
if (!ctype_xdigit(\$z)) return false;
\$tmp = \$v[\$z{0}] * 16 + \$v[\$z{1}];
\$r .= chr(\$tmp);
}
return \$r;
}
\$x0 = "40 3E 51 EB 85 1E B8 52";
\$x1 = hexreverse(\$x0); /* I need this: OS = Linux */
\$x2 = hexify(\$x1); /* reverse the for() loop; */
if (\$x2 === false) die("Invalid input\n");
\$x3 = unpack("d", \$x2); /* reverse the pack() call */
\$y = \$x3[1];
var_dump(\$y); /* dump() the final value. */
?>

--

http://www.dodgeit.com/ == ** ## !! !! ## ** == TEXT-ONLY mail to the complete "Reply-To:" address ("My Name" <my@address>) may bypass the spam filter. I will answer all pertinent mails from a valid

Jul 17 '05 #4
Chung Leong wrote:
Why write your own hex decoder when there's good old sscanf()?

<snip>

Much better :-)
Thanks Chung!

--
== ** ## !! !! ## ** ==
bypass the spam filter. I will answer all pertinent mails from a valid address.
Jul 17 '05 #5
Many thanks to all that contributated...
I now have a solution that works.
....and I understand hex and its conversion way better.

David

"DvGrimm" <dg*********@hotmail.com> wrote in message
news:%Ttod.200465\$9b.50368@edtnps84...
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;
}

Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.