Re: Base36

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: William Stacey [MVP] (staceywREMOVE_at_mvps.org)
Date: 11/08/04


Date: Sun, 7 Nov 2004 22:09:38 -0500

Thanks Justin. Any chance you could add long and short support, and maybe
arbitrary byte[]s? TIA
For byte[]s I was thinking just converting each byte to base36, but that
results in 2 char min after dec 36. So maybe need to take 4 or 8 bytes at a
time and convert to int or long and convert that to a base to leverage the
resulting chars better - not sure. Any thoughts?

-- 
William Stacey, MVP
http://mvp.support.microsoft.com
"Justin Rogers" <Justin@games4dotnet.com> wrote in message
news:Om9Pw9RxEHA.1260@TK2MSFTNGP12.phx.gbl...
> Code-Only: Arbitrary alphabet encoding (aka BaseN encoding) for base2
through
> base36.
>
> The notes are extensive as to the direction the library may or may not go
> depending on what
> problems people are trying to solve. What I've realized is that there are
a
> number of additional
> and interesting problems associated with alphabet encoding, such as
permuations,
> cyclic
> rotations, error correction, and the like that may be interesting to build
into
> the libraries. An
> example of an error correction alphabet would be the base32 encoding which
> removes
> characters that may be confused for other characters when read by a human.
>
>
> -- 
> Justin Rogers
> DigiTec Web Consultants, LLC.
> Blog: http://weblogs.asp.net/justin_rogers
>
> "Roy Fine" <rlfine@twt.obfuscate.net> wrote in message
> news:unkF9yRxEHA.2192@TK2MSFTNGP14.phx.gbl...
> > William
> >
> > The other base was base 26 and used *just* the uppercase alphabetic
> > characters (A..Z).  The only change would be to specify the token set of
the
> > number set and the weights of each position.  For the Base26 case, it
was
> > this:
> >
> > /* ***************** */
> > public class BASE32{
> > static string tokens = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
> > static long [] powers =
> >
{1L,26L,26L*26L,26L*26L*26L,26L*26L*26L*26L,26L*26L*26L*26L*26L,26L*26L*26L*
> >
26L*26L*26L,26L*26L*26L*26L*26L*26L*26L,26L*26L*26L*26L*26L*26L*26L*26L,26L*
> >
26L*26L*26L*26L*26L*26L*26L*26L,26L*26L*26L*26L*26L*26L*26L*26L*26L*26L,26L*
> > 26L*26L*26L*26L*26L*26L*26L*26L*26L*26L};
> > ...
> > ...
> > }
> > /* ***************** */
> >
> > conversion is each direction is based on the tokens and powers arrays.
the
> > first entry in the tokens aray always corresponds to the empty or zero
> > value, etc.
> >
> > happy to help
> > roy
> >
> >
> > "William Stacey [MVP]" <staceywREMOVE@mvps.org> wrote in message
> > news:u$MPFPQxEHA.3908@TK2MSFTNGP12.phx.gbl...
> >> Hey thanks a lot Roy.  Care to post the other base as well?  Either
way,
> >> thanks again!!
> >>
> >> -- 
> >> William Stacey, MVP
> >> http://mvp.support.microsoft.com
> >>
> >> "Roy Fine" <rlfine@twt.obfuscate.net> wrote in message
> >> news:#wPFSCQxEHA.3976@TK2MSFTNGP09.phx.gbl...
> >> > William,
> >> >
> >> > this is something that i did some time ago - actually for a different
> >> base,
> >> > but it was easy enough to change  to handle base32.
> >> >
> >> > you did not specify the symbol set for your number base - i will
assume
> >> > 0,1,2,3... X,Y,Z.  if yours is different, change the tokens string
> >> > accordingly.
> >> >
> >> > for performance reasons, the weights of the digits are computed at
> > compile
> >> > time.
> >> >
> >> > note - there is absolutely no error checking, and it handles only
> > positive
> >> > values, and assumes that all character codes are upper case.
> >> >
> >> > regards
> >> > roy fine
> >> >
> >> >
> >> > namespace CONVERSION{
> >> > // handles positive only values up to 4,738,381,338,321,616,896 - 1;
> >> > public class BASE32{
> >> > static string tokens = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
> >> > static long [] powers =
> >> >
> >>
> >
{1L,36L,36L*36L,36L*36L*36L,36L*36L*36L*36L,36L*36L*36L*36L*36L,36L*36L*36L*
> >> >
> >>
> >
36L*36L*36L,36L*36L*36L*36L*36L*36L*36L,36L*36L*36L*36L*36L*36L*36L*36L,36L*
> >> >
> >>
> >
36L*36L*36L*36L*36L*36L*36L*36L,36L*36L*36L*36L*36L*36L*36L*36L*36L*36L,36L*
> >> > 36L*36L*36L*36L*36L*36L*36L*36L*36L*36L};
> >> >
> >> > public static string ToString(long lval){
> >> >  int maxStrLen = powers.Length;
> >> >  long curval = lval;
> >> >  char [] tb = new char[maxStrLen];
> >> >  int outpos = 0;
> >> >  for(int i=0; i<maxStrLen; i++){
> >> >    long pval = powers[maxStrLen - i - 1];
> >> >    int pos = (int)(curval / pval);
> >> >    tb[outpos++] = tokens.Substring(pos,1).ToCharArray()[0];
> >> >    curval = curval % pval;
> >> >    }
> >> >  if(outpos==0) tb[outpos++] = '0';
> >> >  return new string(tb,0,outpos).TrimStart('0');
> >> > }
> >> >
> >> > public static long ToLong(string t){
> >> >  long ival = 0;
> >> >  char [] tb = t.ToCharArray();
> >> >  for(int i=0; i<tb.Length; i++){
> >> >    ival += powers[i]*tokens.IndexOf(tb[tb.Length-i-1]);
> >> >    }
> >> >  return ival;
> >> >  }
> >> > }
> >> > }
> >> >
> >> > "William Stacey [MVP]" <staceywREMOVE@mvps.org> wrote in message
> >> > news:uc3%23MkOxEHA.1988@TK2MSFTNGP12.phx.gbl...
> >> > > Anyone have a c# Base10ToBase36 and Base36ToBase10 conversion
> > routines?
> >> > TIA
> >> > >
> >> > > -- 
> >> > > William Stacey, MVP
> >> > > http://mvp.support.microsoft.com
> >> > >
> >> >
> >> >
> >>
> >
> >
>
>


Relevant Pages

  • Re: Forth 200x, S\q
    ... Is it true that for \xAB the two hex characters A and B should ... according to 3.2.1.2 Digit conversion. ... Digit conversion). ...
    (comp.lang.forth)
  • Re: Oracle 11 Server and Unicode UTF-8
    ... every field with too much characters (more than 4000 bytes UTF8 code) can ... columns if you have already database with data in it and then add conversion ... That should allow 3 UTF-8 characters to be stored in the ... I haven't found a good reference for how to best accomplish the conversion ...
    (comp.databases.oracle.server)
  • Re: Forth 200x, S\q
    ... Is it true that for \xAB the two hex characters A and B should ... according to 3.2.1.2 Digit conversion. ... Digit conversion). ...
    (comp.lang.forth)
  • Re: Bug in vstudio.NET 2003 codecvt facet
    ... > from single char characters to single wchar_t characters, ... > character string, see below. ... >> string but the conversion fails. ... > _cpp_isleadbyte uses the global locale rather than the specific locale ...
    (microsoft.public.vc.stl)
  • Re: Using Writing Heads/2
    ... after input when you hit a conversion key (on some other systems this ... I can't get kana characters to convert into kanji, ... No, unfortunately, Unicode support is left up to the application. ... I'm considering the idea of writing a Japanese IME, ...
    (comp.os.os2.apps)