|
Prev: tr/// broken?
Next: access denied
From: SnakeyJakey on 21 Apr 2006 10:30 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
From: A. Sinan Unur on 21 Apr 2006 10:44 "SnakeyJakey" <tom.jacobs(a)gmail.com> wrote in news:1145629807.500971.28570 @i40g2000cwc.googlegroups.com: > in C a long is a 64bit int It can be, but there is no such requirement. Sinan -- A. Sinan Unur <1usa(a)llenroc.ude.invalid> (remove .invalid and reverse each component for email address) comp.lang.perl.misc guidelines on the WWW: http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
From: SnakeyJakey on 21 Apr 2006 11:46 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
From: David Squire on 21 Apr 2006 11:58 SnakeyJakey wrote: > but doesnt the explanation mark in L! tell pack to use whatever size > for long the current computer uses, ie 64? > What are you talking about? To whom are you responding? Please quote context. See http://www.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html Regards, DS
From: jl_post@hotmail.com on 21 Apr 2006 13:08 SnakeyJakey wrote: > > $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
|
Pages: 1 Prev: tr/// broken? Next: access denied |