2

I have a string that is essentially binary

my $string = "000110";

I've been trying to use encode_base64 but that encodes strings, if im reading the documentation correctly.

my $j = MIME::Base64->encode_base64($string);
print "$j\n"; # should print 'A'
>> TUlNRTo6QmFzZTY000000

How can I achive this in perl? the string is expected to be ~120 binary bits in length. I'd rather not use any modules that are not installed with perl by default, the target audience for this script is not familiar with the shell.

Edit: A lot of the answers to this question have been surrounded about strings, not actual numbers, there was one solution I found, but it required Math::BaseCalc module to be installed.

Edit2: Essentially, if i have

my $binary_string = "000110";

i would like to have it encoded in base64 (as a number), so it returns

>>G  # for this case (binary number 000110 to base64 number = G)
3
  • When you say "essentially binary" do you mean that it is actually binary? Commented Sep 12, 2014 at 17:46
  • yes, $string = "1001011" for example, so the string only has ones or zeros, each character of the string is to be a binary bit Commented Sep 12, 2014 at 17:46
  • so long as it takes the string and encodes it as a base64, but treats it as a number, not as a string as encode_base64 does Commented Sep 12, 2014 at 17:50

1 Answer 1

5

base64 is an algorithm that converts strings of 8-bit bytes/characters. Anything else must be packed into bytes.

You already have a string, but you could be more space-efficient by packing the 120 bits into 15 bytes using the following:

my $base64 = encode_base64(pack("B*", $binary), "");

The inverse operation is

my $binary = unpack("B*", decode_base64($base64));

For example,

$ perl -MMIME::Base64 -E'say encode_base64(pack("B*", $ARGV[0]), "")' \
   0100000101000010
QUI=

$ perl -MMIME::Base64 -E'say unpack("B*", decode_base64($ARGV[0]))' \
   QUI=
0100000101000010

If you have actually have a number of bits that's not divisible by 8, you can prefix the string with the number of bits.

my $base64 = encode_base64(pack("CB*", length($binary), $binary), "");

The inverse operation is

my ($length, $binary) = unpack("CB*", decode_base64($base64));
substr($binary, $length) = "";
Sign up to request clarification or add additional context in comments.

8 Comments

my $base64 = encode_base64 pack 'B*', "00000000"; returns AA== not A
@Yusuf Ali, Correct. And that's a good thing, because A is not valid base64.
i tried with 00000000, which should return 'A', but doesnt
Not so. A is not valid base64.
No. Again, base64 is an algorithm that encodes a sequence of bytes. 3 bytes => 4 ASCII chars. (Padding is used to round up to a multiple of 3 bytes.)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.