Math problem - converting between arbitrary bases in perl - help!

Discussion in 'Perl' started by ! aaa, May 27, 2004.

  1. ! aaa

    ! aaa Guest

    Hi all - I've been trying to write a small sub to convert form an input base
    (eg: 16) to an output base (eg:64) and back.

    The reason is that I need to communicate tristate (base 3) data efficiently
    via DNS (base 37 a-z0-9 and '-').

    I *could* do it by converting into a "bigmath binary" structure I guess, but
    I thought there might be a more elegant way...

    but such a solution eludes me.

    Can anyone think how to approach the problem?

    Below is my NON-working attempt. I thought of creating a "buffer" (I called
    it $remainder) through which I can
    convert incoming stuff to the outgoing stuff, but (A) I'm not sure if this
    is even possible, and (B) if so, I'm lost
    near the end, where I'm "nibbling off" parts of the $remainder variable as I
    output results. In short. I'm wrong
    there someplace :)

    # Sorry I forgot to comment it :))




    #!perl

    use strict;

    # my($from_base)=10; my($to_base)=94; my($data)='92356234';
    # my($from_base)=10; my($to_base)=16; my($data)='283036414'; # 283036414
    should be 10DECAFE in hex
    my($from_base)=16; my($to_base)=10; my($data)='C0DECAFE'; # 283036414 should
    be 10DECAFE in hex

    my $b2 = &baseconv($from_base,$to_base,$data);
    print "Base$from_base($data)=Base$to_base($b2)\n";

    sub baseconv {
    my($from_base,$to_base,$data)=@_;
    my
    $collseq='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!"#$
    %&\'()*+,-./:;<=>?@[\]^_`{|}~'; # 94 "digits"
    my $remainder=0; my $pow=0; my $ret=''; my $tmp; my $mop=0;
    while(($data ne '')||($remainder)) {
    $remainder+=index($collseq,chop $data)*($from_base**($pow++))
    while(($data ne '')&&($remainder<(($to_base*$from_base)**2)));
    print "remainder=$remainder\n";

    my($tgt)=($to_base*$from_base)**2; $mop=0;
    while((($tgt % $from_base)==0)||(($data eq '')&&($remainder>0))) {
    $pow--; $tgt=$tgt/$from_base;
    $tmp=$remainder % $to_base;
    $ret.=substr($collseq,$tmp,1); $remainder=($remainder-$tmp)/$to_base;
    print "ret=$ret data=$data remainder=$remainder tgt=$tgt\n";
    exit(0) if($pow<-10);
    }

    # while(($remainder>=$to_base)||($data eq '')) { $tmp=$remainder %
    $to_base; $ret.=substr($collseq,$tmp,1); $remainder=($remaind
    er-$tmp)/$to_base; }


    print "ret=$ret\n";
    }
    return reverse($ret);
    }
     
    ! aaa, May 27, 2004
    #1
    1. Advertising

  2. ! aaa

    ! aaa Guest

    p.s. I forgot to mention - arbitrary sized numbers - eg: not restricted to
    2^32 (base 10)
    or other machine constraints.

    *** See also my new post in comp.lang.perl.misc. ***
     
    ! aaa, May 28, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ferran
    Replies:
    1
    Views:
    1,121
    Victor Bazarov
    Nov 28, 2003
  2. Honestmath
    Replies:
    5
    Views:
    578
    Honestmath
    Dec 13, 2004
  3. Alex Buell

    Converting between number bases

    Alex Buell, Aug 8, 2006, in forum: C++
    Replies:
    2
    Views:
    584
    Robbie Hatley
    Aug 8, 2006
  4. ! aaa
    Replies:
    4
    Views:
    164
    Jay Tilton
    May 28, 2004
  5. VK
    Replies:
    15
    Views:
    1,278
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page