Paul G. Allen wrote:
On Wed, 2007-03-07 at 10:10 -0800, Stewart Stremler wrote:
begin  quoting Paul G. Allen as of Wed, Mar 07, 2007 at 09:18:52AM -0800:
It's been a while (years?) since I've done anything in Perl. I need to
convert an ASCII string of ones and zeros to a double. (Actually, I
don't need to, but one of our engineers does.)
As in a floating point?

The exact request the I got was:

"I spent more than 4 hours to convert the following binary string to
double number using pack and unpack.
0100000000000110010111111100101100100101111111001011001001100000"

He said he was unsuccessful.

Here's the Perl pack tutorial:
http://perldoc.perl.org/perlpacktut.html

And this little excursion reminded me of just why I hate Perl so much.

-a

---<Cut Here>---

sub DumpString {
   my @a = unpack('C*',$_[0]);
   my $o = 0;
   while (@a) {
       my @b = splice @a,0,16;
       my @d = map sprintf("%03d",$_), @b;
       my @x = map sprintf("%02x",$_), @b;
       my $c = substr($_[0],$o,16);
       $c =~ s/[[:^print:]]/ /g;
       printf "%6d %s\n",$o,join(' ',@d);
       print " "x8,join('  ',@x),"\n";
       print " "x9,join('   ',split(//,$c)),"\n";
       $o += 16;
   }
}

print "Converting: 1e+276\n";
$a = pack("d", 1e+276);
DumpString($a);

($b) = unpack("d", $a);
print "Original value: $b \n";

print "Converting 
0100000000000110010111111100101100100101111111001011001001100000\n";

# Big endian or little endian???
$c = pack('B64', 
"0100000000000110010111111100101100100101111111001011001001100000");
DumpString($c);

($d) = unpack('d', $c);
print "Converted binary string: $d", "\n";

--
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-lpsg

Reply via email to