in reply to Binary string to base64

I'm in over my head here, but here is a way to get the compression down to 5 characters.
#results 0000000100100011010001010110011110001001101010111100110111101111 -> + iL1@;
This almost works. I can only get the last half of the bin string back, but maybe the gurus can see the packing error. Maybe needs 64 bit numbers or Math::BigINT?
#!/usr/bin/perl use strict; use Math::Base85; my $str = "00000001001000110100010101100111100010011010101111001101111 +01111"; #binary to decimal conversion my $num = unpack("N", pack("B32", substr("0" x 32 . $str, -32))); print "$num\n"; my $m = Math::Base85::to_base85($num); print "$str ->\t$m\n"; #decode it ############################################## my $q = Math::Base85::from_base85($m); print "back to decimal ->\t$q\n"; #I'm losing a 32 bit chunk here my $str1 = unpack("B32", pack("N", $q)); print "back to bin ->\n", $str1,"\n";

I'm not really a human, but I play one on earth. flash japh

Replies are listed 'Best First'.
Re^2: Binary string to base64
by albert (Monk) on Jun 16, 2006 at 18:28 UTC
    Thanks for the suggestion:

    Working within Math::BigInt, it is easy to make the integer by prepending the string with '0b'. Then, between Math::Base85 and Math::BigInt, it is easy to go back and forth, though one needs to prepend '0's after the decoding to get back to the original length.

    use Math::Base85 qw(from_base85 to_base85); $str = '0b000000010010001101000101011001111000100110101011110011011110 +1111'; $bint = Math::BigInt->new($str); $b85str = to_base85($bint); $bint2 = from_base85($b85str); print $bint2->as_bin();
    This does exactly what I want now. And I get slightly more efficiency than with base64.

    -albert

      Hi, I was working on the same thing, but I added a '0b1' so that the leading zeroes in the binary wouldn't get truncated. It condenses it down to 10 characters. Here's my attempt:
      #!/usr/bin/perl use Math::BigInt; use Math::Base85; my $str = '00000001001000110100010101100111100010011010101111001101111 +01111'; print "$str\n"; #prepend a 0b1 to signal binary and contain leading zeroes my $str_p = '0b1'.$str; print "$str_p\n\n"; my $bignum = Math::BigInt->new($str_p); my $hex = $bignum->as_hex(); print "hex $hex\n\n"; my $hex85 = Math::Base85::to_base85($hex); print "hex85-> $hex85\n\n"; my $hex_back = Math::Base85::from_base85($hex85); print "hex_back-> $hex_back\n"; my $bignum1 = Math::BigInt->new($hex_back); my $b_back = $bignum1->as_bin(); #strip off the '0b1' $b_back = substr($b_back, 3); print "out-> $b_back\n"; print "in -> $str\n";

      I'm not really a human, but I play one on earth. flash japh