Skip to main content
added 917 characters in body
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k
$ echo AB | perl -lpe '$_=unpack"B*"' 0100000101000010 $ echo 0100000101000010 | perl -lpe '$_=pack"B*",$_' AB 
  • -e expression evaluate the given expression as perl code
  • -p: sed mode. The expression is evaluated for each line of input, with the content of the line stored in the $_ variable and printed after the evaluation of the expression.
  • -l: even more like sed: instead of the full line, only the content of the line (that is, without the line delimiter) is in $_ (and a newline is added back on output). So perl -lpe code works like sed code except that it's perl code as opposed to sed code.
  • unpack "B*" works on the $_ variable by default and extracts its content as a bit string walking from the highest bit of the first byte to the lowest bit of the last byte.
  • pack does the reverse of unpack. See perldoc -f pack for details.

With spaces:

$ echo AB | perl -lpe '$_=join " ", unpack"(B8)*"' 01000001 01000010 $ echo 01000001 01000010 | perl -lape '$_=pack"(B8)*",@F' AB 

(it assumes the input is in blocks of 8 bits (0-padded)).

With unpack "(B8)*", we extract 8 bits at a time, and we join the resulting strings with spaces with join " ".

$ echo AB | perl -lpe '$_=unpack"B*"' 0100000101000010 $ echo 0100000101000010 | perl -lpe '$_=pack"B*",$_' AB 

With spaces:

$ echo AB | perl -lpe '$_=join " ", unpack"(B8)*"' 01000001 01000010 $ echo 01000001 01000010 | perl -lape '$_=pack"(B8)*",@F' AB 

(it assumes the input is in blocks of 8 bits (0-padded)).

$ echo AB | perl -lpe '$_=unpack"B*"' 0100000101000010 $ echo 0100000101000010 | perl -lpe '$_=pack"B*",$_' AB 
  • -e expression evaluate the given expression as perl code
  • -p: sed mode. The expression is evaluated for each line of input, with the content of the line stored in the $_ variable and printed after the evaluation of the expression.
  • -l: even more like sed: instead of the full line, only the content of the line (that is, without the line delimiter) is in $_ (and a newline is added back on output). So perl -lpe code works like sed code except that it's perl code as opposed to sed code.
  • unpack "B*" works on the $_ variable by default and extracts its content as a bit string walking from the highest bit of the first byte to the lowest bit of the last byte.
  • pack does the reverse of unpack. See perldoc -f pack for details.

With spaces:

$ echo AB | perl -lpe '$_=join " ", unpack"(B8)*"' 01000001 01000010 $ echo 01000001 01000010 | perl -lape '$_=pack"(B8)*",@F' AB 

(it assumes the input is in blocks of 8 bits (0-padded)).

With unpack "(B8)*", we extract 8 bits at a time, and we join the resulting strings with spaces with join " ".

added 59 characters in body
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k
$ echo AB | perl -lpe '$_=unpack"B*"' 0100000101000010 $ echo 0100000101000010 | perl -lpe '$_=pack"B*",$_' AB 

With spaces:

$ echo AB | perl -lpe '$_=join " ", unpack"(B8)*"' 01000001 01000010 $ echo 01000001 01000010 | perl -lape '$_=pack"(B8)*",@F' AB 

(it assumes the input is in blocks of 8 bits (0-padded)).

$ echo AB | perl -lpe '$_=unpack"B*"' 0100000101000010 $ echo 0100000101000010 | perl -lpe '$_=pack"B*",$_' AB 

With spaces:

$ echo AB | perl -lpe '$_=join " ", unpack"(B8)*"' 01000001 01000010 $ echo 01000001 01000010 | perl -lape '$_=pack"(B8)*",@F' AB 
$ echo AB | perl -lpe '$_=unpack"B*"' 0100000101000010 $ echo 0100000101000010 | perl -lpe '$_=pack"B*",$_' AB 

With spaces:

$ echo AB | perl -lpe '$_=join " ", unpack"(B8)*"' 01000001 01000010 $ echo 01000001 01000010 | perl -lape '$_=pack"(B8)*",@F' AB 

(it assumes the input is in blocks of 8 bits (0-padded)).

added 184 characters in body
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k
$ echo AAB | perl -lpe '$_=unpack"B*"' 010000010100000101000010 $ echo 010000010100000101000010 | perl -lpe '$_=pack"B*",$_' AAB 

With spaces:

$ echo AB | perl -lpe '$_=join " ", unpack"(B8)*"' 01000001 01000010 $ echo 01000001 01000010 | perl -lape '$_=pack"(B8)*",@F' AB 
$ echo A | perl -lpe '$_=unpack"B*"' 01000001 $ echo 01000001 | perl -lpe '$_=pack"B*",$_' A 
$ echo AB | perl -lpe '$_=unpack"B*"' 0100000101000010 $ echo 0100000101000010 | perl -lpe '$_=pack"B*",$_' AB 

With spaces:

$ echo AB | perl -lpe '$_=join " ", unpack"(B8)*"' 01000001 01000010 $ echo 01000001 01000010 | perl -lape '$_=pack"(B8)*",@F' AB 
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k
Loading