sub encode{ my $mode = shift; my $area = shift; my $subarea = shift; my $str; my $bitstr=unpack("b4",pack("v",$mode)). unpack("b16",pack("v",$area)). unpack("b16",pack("v",$subarea)); for (0..5){ my $val=unpack('c',pack('b6',substr($bitstr,$_*6,6))); if ($val<10){ $str.=pack('c',$val+48); }elsif ($val<36){ $str.=pack('c',$val+65-10); }elsif ($val<62){ $str.=pack('c',$val+97-36); }elsif ($val == 63){ $str.=pack('c',37); }elsif ($val == 64){ $str.=pack('c',45); } } $str; } sub decode{ my $str = shift; my $bitstr; for (split //, $str){ my $item = unpack('c', $_); for ($item){ $val = 64, last if $item == 45; $val = 63, last if $item == 37; $val = $item-97+36, last if $item >= 97; $val = $item-65+10, last if $item >= 65; $val = $item-48, last if $item >= 48; } $bitstr .= unpack("b6",pack("v",$val)); } $mode = unpack('c',pack('b4',substr( $bitstr,0,4))); $area = unpack('v',pack('b16',substr( $bitstr,4,16))); $subarea = unpack('v',pack('b16',substr( $bitstr,20,16))); ($mode, $area, $subarea); } $encoded=encode(15,13,13); $decoded=join ', ', decode( $encoded ); print "Encoded: $encoded\nDecoded array: $decoded\n";