CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Advanced:
Problem converting 64bit binary (qword) to float (Win32)


New User

Mar 25, 2007, 9:32 PM

Post #1 of 4 (4259 views)
Problem converting 64bit binary (qword) to float (Win32) Can't Post

I need to convert 64bit (8byte) binary data to double-precision floating point.

I need this: 0100000001001001000000000000000000000000000000000000000000000000
To convert to floating point 50.00

I tried unpack, but it's built-in float is only 32bit. See the test code below where unpack gives the same results for all 64 bits as when only using the first 32 bits.

my $binary64 = '0100000001001001000000000000000000000000000000000000000000000000'; # Hex: 4049000000000000 Decimal: 50 
my $unpacked64 = unpack('d',$binary64);
print "64 Bits: $binary64\n";
print "Unpacked with 'd': $unpacked64\n\n";

# Compare to same string as above, but truncated to 32bits
my $binary32 = substr($binary64,0,32); # Hex: 40490000
my $unpacked32 = unpack('d',$binary32);
print "32 Bits: $binary32\n";
print "Unpacked with 'd': $unpacked32\n";


Bits: 0100000001001001000000000000000000000000000000000000000000000000 
Unpacked with 'd': 1.39804328609537e-076

Bits: 01000000010010010000000000000000
Unpacked with 'd': 1.39804328609537e-076

(The 64bit number should be 50 in decimal)

I'm using Bit::Vector for my 64bit long conversions, but that doesn't support floats.

While Math::BigInt supports binary input (just preface the binary string with '0b'), that doesn't work for Math::BigFloat

my $binary64 = '0100000001001001000000000000000000000000000000000000000000000000'; 

use Math::BigInt;
my $bigInt = Math::BigInt->new('0b'.$binary64);
print "64bits as INT: $bigInt\n";

use Math::BigFloat;
my $bigFloat = Math::BigFloat->new('0b'.$binary64);
print "64bits as Float: $bigFloat\n";


64bits as INT: 4632233691727265792 
Can't use an undefined value as a SCALAR reference at c:/Perl/lib/Math/ line 177.

Does anyone have any suggestions? I just hit a brick wall on my project.


Mar 25, 2007, 11:26 PM

Post #2 of 4 (4255 views)
Re: [JonHodgson] Problem converting 64bit binary (qword) to float (Win32) [In reply to] Can't Post

isn't 50 in binary:


or just:


New User

Mar 26, 2007, 12:22 AM

Post #3 of 4 (4252 views)
Re: [KevinR] Problem converting 64bit binary (qword) to float (Win32) [In reply to] Can't Post

If 50 is stored as a 4 byte integer or long is stored then 110010 would be correct.

However, those data formats are incapable of storing widely varing values, especially with high precision decimal places.

That's why I'm using a double-precision floating point format. While I gave the simple example of 50, my data will range widely and I must use that datatype. So my number is actually more like 50.0000000000 - which is not the same as 110010

Changing the data type is not an option. I need a way to use these in PERL.


Mar 26, 2007, 1:10 AM

Post #4 of 4 (4251 views)
Re: [JonHodgson] Problem converting 64bit binary (qword) to float (Win32) [In reply to] Can't Post

ask on the perlmonks site:

probably someone there will be able to help you, your question is a bit out of my range of perl knowledge.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives