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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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


In reply to Re: Question: methods to transfer a long hexadicimal into shorter string by QM
in thread Question: methods to transfer a long hexadicimal into shorter string by lihao

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-24 07:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found