472,810 Members | 3,970 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,810 software developers and data experts.

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. */
?>

--
Mail sent to my "From:" address is publicly readable at 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 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
other direction, which you can probably adapt to your situation fairly
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. */
?>

--
Mail sent to my "From:" address is publicly readable at

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

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

<snip>

Much better :-)
Thanks Chung!

--
Mail sent to my "From:" address is publicly readable at 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 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.

Similar topics

31
by: JS | last post by:
We have the same floating point intensive C++ program that runs on Windows on Intel chip and on Sun Solaris on SPARC chips. The program reads the exactly the same input files on the two platforms....
5
by: Anton Noll | last post by:
We are using Visual Studio 2003.NET (C++) for the development of our software in the fields digital signal processing and numerical acoustics. One of our programs was working correctly if we are...
687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
24
by: j0mbolar | last post by:
C supports single precision floating point and double precision floating point but does it support fixed floating point? i've read that fixed floating point is more accurate than single precision...
7
by: Vinoth | last post by:
I'm working in an ARM (ARM9) system which does not have Floating point co-processor or Floating point libraries. But it does support long long int (64 bits). Can you provide some link that would...
15
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
4
by: jacob navia | last post by:
Hi people I continue to work in the tutorial for lcc-win32, and started to try to explain the floating point flags. Here is the relevant part of the tutorial. Since it is a difficult part, I...
32
by: ma740988 | last post by:
template <class T> inline bool isEqual( const T& a, const T& b, const T epsilon = std::numeric_limits<T>::epsilon() ) { const T diff = a - b; return ( diff <= epsilon ) && ( diff >= -epsilon );...
39
by: rembremading | last post by:
Hi all! The following piece of code has (for me) completely unexpected behaviour. (I compile it with gcc-Version 4.0.3) Something goes wrong with the integer to float conversion. Maybe somebody...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.