Skip to content

Instantly share code, notes, and snippets.

@dbb
Created March 28, 2011 22:19
Show Gist options
  • Save dbb/891424 to your computer and use it in GitHub Desktop.
Save dbb/891424 to your computer and use it in GitHub Desktop.
print '0b01110010 0b01100001 0b01100100'
0b01110010 0b01100001 0b01100100
perl -e 'print chr(0b01100001)'
a
print '0b01110010 0b01100001 0b01100100' | perl -lane 'print $_ for @F'
0b01110010
0b01100001
0b01100100
print '0b01110010 0b01100001 0b01100100' | perl -lane 'for (@F) { $s=chr($_); print $s;}'
(NO OUTPUT)
perl -le '@a=qw(0b01110010 0b01100001 0b01100100); print chr($_) for @a;'
(NO OUTPUT)
@telemachus
Copy link

perl -le '@a=(0b01110010, 0b01100001, 0b01100100); print chr($_) for @a;' # prints => r, a, d one per line

Perl interprets the items as strings in the second and third example, and chr(string) returns undef, I suppose. So you need a way to convince Perl that's a number. The function oct will do that:

echo '0b01110010 0b01100001 0b01100100' | perl -lane 'print chr(oct($_)) for @F'

See here: http://stackoverflow.com/questions/483655/how-do-i-convert-a-binary-string-to-a-number-in-perl

@dbb
Copy link
Author

dbb commented Mar 29, 2011

Probably the first time I've ever been annoyed by dynamic typing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment