long (64bit) to binary

S

SnakeyJakey

i am trying to convert char, short in float double and long number
(they probably are currently in strings form) to binary so that i can
print out hte binary representation of each. i am currently using the
following:

$binary_int = unpack("B32", pack("I", $int));
$binary_short = unpack("b16", pack("S", $short));
$binary_char = unpack("B8", pack("C", $char));
$binary_float = unpack("b32", pack("f", $float));
$binary_double = unpack("b64", pack("d", $double));
$binary_long = unpack("b64", pack("L!", $long));

this work great for all but long. when $long can be represented in
32bit it does so but once it get larger $binary_long just saturates to
all 1's. i have tried various things in pack instead of L! like L N
etc but have not found anything that will represent a 64bit integer.
having googled about it looks like in perl they call 64bit ints quad
and they are only availiable on certain systems. im not sure if this is
thw case since in C a long is a 64bit int so this pc (x86) should be
able to support such a number.

How can i solve this.

also this prints the number from lsb to msb (a complete mirror image to
the usual little endia was of representing number msb->lsb) how can i
change this? changing from lowercase to upper case, and vise vera,
changes the endianess but still the least sig byte comes first and not
the most. is there an easy way to correct this?

many thanks
 
S

SnakeyJakey

but doesnt the explanation mark in L! tell pack to use whatever size
for long the current computer uses, ie 64?

it it helps i will descript why i want this. i am reading in all global
variables from some C source and also any initialised value too. i need
to have this value represented as binary.

there is one work around i could use (although not sure as quite to
achive it) is to split the 64bit long into two 32bit ints and represent
each of these. im not sure if this is easier. can i pack my (string) to
8 digit hex and then extract each 4digit and convert them to binary. im
thing this would lead to the same problem since i would still have to
pack L! and then unpack to hex
 
J

jl_post

SnakeyJakey said:
$binary_long = unpack("b64", pack("L!", $long));

when $long can be represented in 32bit it does so but once it
get larger $binary_long just saturates to all 1's.


Dear SnakeyJakey,

I don't know if this is the solution you're looking for, but I can
provide you a work-around that uses the standard Math::BigInt module.
Try this script:


#!/usr/bin/perl
use strict;
use warnings;
use Math::BigInt;

my $bigNumber = Math::BigInt->new(2) ** 55; # too big for 32-bits
print "\$bigNumber = $bigNumber\n";

# Create a temporary number (that gets destroyed
# while calculating the bits):
my $temporaryNumber = $bigNumber;

my $bits = "";
foreach (1 .. 64)
{
$bits .= $temporaryNumber % 2;
$temporaryNumber >>= 1;
}

# Bits are reversed, so flip them around:
$bits = reverse($bits);

print "Bits (big-endian):\n$bits\n";

# Convert to little-endian:
$bits = reverse($bits);
$bits =~ s/(.{8})/scalar reverse($1)/eg;

print "Bits (little-endian):\n$bits\n";

__END__


Like I said, I don't know if this is the solution you're looking
for, but it does give you the bit representation of 64-bit integers in
both little- and big-endian order. (I can't guarantee how it will work
for negative integers, but testing it on my platform with the line:

my $bigNumber = Math::BigInt->new(-3);

gives me the output:

$bigNumber = -3
Bits (big-endian):
1111111111111111111111111111111111111111111111111111111111111101
Bits (little-endian):
1111110111111111111111111111111111111111111111111111111111111111

which appears to be correct, as far as I can tell.)

I hope this helps.

-- Jean-Luc
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top