in reply to Handling binary

#!/usr/bin/perl # http://perlmonks.org/?node_id=1141773 use strict; use warnings; print handlingbinary( "139686DA20C1" ), "\n"; sub handlingbinary { return join '', ('A'..'Z')[ map { oct "0b$_" } (unpack 'B*', pack 'H*', shift()) =~ /.{5}/g ] }

Prints:

COLINWRAY

Hi Colin! (Am I right? If not, let me know :)

Replies are listed 'Best First'.
Re^2: Handling binary
by Anonymous Monk on Sep 12, 2015 at 16:19 UTC

    I missed the part about having to generate these strings, here you go:

    #!/usr/bin/perl # http://perlmonks.org/?node_id=1141773 use strict; use warnings; my $answer = handlingbinary( "139686DA20C1" ); print "$answer\n"; print tofivebit( 'ANONYMOUS MONK' ), "\n"; print tofivebit( 'COLINWRAY' ), "\n"; sub handlingbinary { return join '', ('A'..'Z')[ map { oct "0b$_" } (unpack 'B*', pack 'H*', shift()) =~ /.{5}/g ] } sub tofivebit { uc unpack 'H*', pack 'B*', shift() =~ tr/A-Z//cdr =~ s/(.)/ sprintf "%05b", ord($1) - ord('A' +) /ger; }

      Many thanks to the three responders, and yes you got it right. The code snippets contain some very welcome insights into this (sometimes) irritating language - at least to an old (retired) C/C++ programmer.

      Why 5 bit ? - not my choice, not my firmware which requires it, but surely to conserve space. There is some 6 bit later in the string because they needed to include digits.

      Hex is a way of life for me.

      Colin