Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Question: methods to transfer a long hexadicimal into shorter string

by QM (Vicar)
on Aug 07, 2009 at 17:16 UTC ( #786847=note: print w/ replies, xml ) Need Help??


in reply to Question: methods to transfer a long hexadicimal into shorter string

In some of my projects I needed to encode some m/[0-9a-z ]+/i strings into the smallest number of bits possible. Here's a little check script to see what various ID numbers or strings convert to:

eval '(exit $?0)' && eval 'exec perl -w -S $0 ${1+"$@"}' && eval 'exec perl -w -S $0 $argv:q' if 0; # The above invocation finds perl in the path, wherever it may be use strict; use warnings; use Math::BigInt; # encode/decode ID numbers our $DECODE_OPT = q/-d/; our $ENCODE_OPT = q/-e/; sub usage { warn "@_\n\n"; warn "usage: $0 [$DECODE_OPT|$ENCODE_OPT] arg arg...\n\n"; warn "\t-e\tencode an ID number into binary/octal/hex/decimal\n"; warn "\t-d\tdecode a binary/octal/hex/decimal into the equivalent +text ID number\n\n"; warn "Notes:\n"; warn "\tvalues must be prefixed with by:\n"; warn "\t\tbinary 0b (\"zero bee\") (or 0B)\n"; warn "\t\toctal 0 (\"zero\")\n"; warn "\t\thex 0x (\"zero ex\") (or 0X)\n"; warn "\nExample: to encode N12345, use this command:\n"; warn "\t$0 $ENCODE_OPT N12345\n"; warn "\n"; die; } usage( "Error: No arguments supplied..." ) unless (@ARGV); usage( "Error: No encode/decode command used..." ) unless ($ARGV[0] =~ + /^($ENCODE_OPT|$DECODE_OPT)$/i); my $opt = $1; shift @ARGV; usage( "Error: Not enough arguments supplied..." ) unless (@ARGV); # use Math::BigInt for arbitrary precision math (32bit integers too sm +all) our $ZERO = new Math::BigInt +0; our %encode; @encode{" ","A".."Z","a".."z","0".."9"} = (0,1..26,1..26,27..36); our %decode; @decode{0..36} = (" ","A".."Z",0..9); our $base = $ZERO + scalar keys %decode; sub encode { my $x = shift; my @x = split '', $x; my $r = $ZERO; for my $c ( @x ) { $r = $r * 37 + $encode{$c}; } return $r; } sub decode { my $x = shift; return "invalid decode" if $x =~ /[a-z]/i; my $r = ''; while ( $x ) { my $LSD = $x % $base; # least significant digit $r = $decode{$LSD} . $r; #prepend $x = ($x - $LSD)/$base; } return $r; } for (@ARGV) { if ( $opt =~ /^$ENCODE_OPT$/i ) { # encode my $e = $ZERO + encode($_); printf "Text: %s\nBinary: %s\nOctal: %s\nHex: %s\nDe +cimal: %s\n\n", $_, $e->as_bin(), $e->as_oct(), $e->as_hex(), $e; } else { # decode printf "Input: %s\nText: %s\n\n", $_, decode($_ + $ZERO); } } exit;

-QM
--
Quantum Mechanics: The dreams stuff is made of


Comment on Re: Question: methods to transfer a long hexadicimal into shorter string
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://786847]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2014-10-21 08:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (98 votes), past polls