hex to double

S

skerrick32

Hello,

I am trying to convert a little endian hex number that represents an 8
byte double to the decimal representation of that double. I found some
code that goes from double to hex but I what I need is the opposite.
Can anyone help? Here is the double to hex code.

my $real = 1/7;
my $packed_double = pack "d", $real;
my $hex_double = sprintf("%04X%04X", unpack("LL", pack("d", $real)));

What I need is the opposite.

Thanks
Eric
 
S

skerrick32

Jim. Thanks for the reply!

It does ot seem to work, though.

The new real is 7.08789492177958e-134
The original 1/7 or 0.142857142857143

my $real = 1/7;
print "real: $real\n";
my $packed_double = pack "d", $real;
my $hex_double = sprintf("%04X%04X", unpack("LL", pack("d", $real)));
my $new_real = unpack('d', pack('H16', $hex_double));
print "new_real: $new_real\n";

Any other thoughts?

Thanks again
Eric
 
S

skerrick32

Hi Jim,

That is weird! It must have something to do with PERL on a win32. I am
using ActiveState PERL and It does not appear to work as it does for
you. Another reason for me to move to Linux! :)

Thanks Jim. I appreciate your help.

Eric
 
H

Harry

(e-mail address removed) wrote...
Hi Jim,

That is weird! It must have something to do with PERL on a win32. I am
using ActiveState PERL and It does not appear to work as it does for
you. Another reason for me to move to Linux! :)

On cygwin, try this (LL changed to NN).
Don't ask me why ;)

$ cat xx.pl
#!/usr/bin/perl -w
use strict;
use warnings;
my $real = 1/7;
my $hex_double = sprintf('%04X%04X', unpack('NN', pack('d', $real)));
print "$real -> $hex_double\n";
my $new_real = unpack('d', pack('H16', $hex_double));
print "$hex_double -> $new_real\n";

$ ./xx.pl
0.142857142857143 -> 922449922449C23F
922449922449C23F -> 0.142857142857143
 
S

Sisyphus

Harry said:
(e-mail address removed) wrote...

Won't necessarily help - it's machine-dependent (as opposed to OS-dependent)
behaviour. I get the same as the OP on both Linux and Windows.
On cygwin, try this (LL changed to NN).
Don't ask me why ;)

$ cat xx.pl
#!/usr/bin/perl -w
use strict;
use warnings;
my $real = 1/7;
my $hex_double = sprintf('%04X%04X', unpack('NN', pack('d', $real)));
print "$real -> $hex_double\n";
my $new_real = unpack('d', pack('H16', $hex_double));
print "$hex_double -> $new_real\n";

$ ./xx.pl
0.142857142857143 -> 922449922449C23F
922449922449C23F -> 0.142857142857143

Yes - that gets things ordered appropriately. It works for me and should
work for the OP.

Note that the hex values are different to what Jim gets. To get the same hex
string as he gets I have to do:

my $hex_double = sprintf("%04X%04X", reverse(unpack("LL", pack("d",
$real))));

Cheers,
Rob
 
J

John W. Krahn

I am trying to convert a little endian hex number that represents an 8
byte double to the decimal representation of that double. I found some
code that goes from double to hex but I what I need is the opposite.
Can anyone help? Here is the double to hex code.

my $real = 1/7;
my $packed_double = pack "d", $real;
my $hex_double = sprintf("%04X%04X", unpack("LL", pack("d", $real)));

What I need is the opposite.

$ perl -le'
$x = 1/7;
$y = unpack "H*", pack "d", $x;
$z = unpack "d", pack "H*", $y;
print for $x, $y, $z;
'
0.142857142857143
922449922449c23f
0.142857142857143


John
 
J

Joe Smith

That is weird! It must have something to do with PERL on a win32. I am
using ActiveState PERL and It does not appear to work as it does for
you. Another reason for me to move to Linux! :)

It has nothing to do with win32.

When run on a big-endian system (Sun SPARC):
perl /tmp/temp.pl
0.142857142857143 -> 3FC2492492492492
3FC2492492492492 -> 0.142857142857143

When run on a little-endian system (Linux on x86):
perl temp.pl
0.142857142857143 -> 924924923FC24924
924924923FC24924 -> 7.08789492177958e-134

Moral of the story: when working with big-endian and little-endian
values, _never_ use L. Use N or V for pack/unpack.

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,286
Latest member
ChristieSo

Latest Threads

Top