Some simple one-liners for encoding and decoding binary-coded decimals (BCD).
# Encode perl -le 'print join " ", map {ord pack "H2", $_} shift =~ /../g' +num # decode perl -le 'print unpack "H*", pack "C*", @ARGV' num1 num2 ... perl -le 'print map {int($_/16).($_%16)} @ARGV' num1 num2 ...
Some examples:
You can use "h" instead of "H" for low nibble first encoding. Single digit components of the number to be encoded should be zero padded.# Encode $ perl -le 'print join " ", map {ord pack "H2", $_} shift =~ /../g +' 019774 1 151 116 # decode $ perl -le 'print unpack "H*", pack "C*", @ARGV' 1 151 116 019774
--
John.
Back to
Cool Uses for Perl