Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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

by QM (Parson)
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;

Quantum Mechanics: The dreams stuff is made of

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://786847]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2017-10-23 21:11 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (285 votes). Check out past polls.