Base36

  • Thread starter Thread starter William Stacey [MVP]
  • Start date Start date
W

William Stacey [MVP]

Anyone have a c# Base10ToBase36 and Base36ToBase10 conversion routines? TIA
 
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*tokens.IndexOf(tb[tb.Length-i-1]);
}
return ival;
}
}
}
 
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 said:
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*tokens.IndexOf(tb[tb.Length-i-1]);
}
return ival;
}
}
}
 
A generic base conversion can be applied by taking an alphabet:

private void char[] alphabet = new char[] {'0','1','2','3','4'}; // Base 5

private string IntegerToBase(int foo, char[] alphabet) {
int base = alphabet.Length;
string baseStr = "";
do {
baseStr = alphabet[foo%base] + baseStr;
foo /= base;
} while(foo > 0);

return baseStr
}

Given any alphabet, you can now go one way from integers to the base.
The reverse is more complicated, but only because certain assumptions
have to be made (converting from a well known type with specific rules
such as an integer is always easier than converting from an arbitrary type
where rules aren't self-imposed within a string).

I'm sure you also need the reverse. I already have numerous parsing
and conversion routines located in the blog space, so I'll try and elevate
this to a blog posting, and then back it with an article that contains links
and short details to all of my parsing routines to make them more
accessible.

--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

William Stacey said:
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 said:
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*tokens.IndexOf(tb[tb.Length-i-1]);
}
return ival;
}
}
}

William Stacey said:
Anyone have a c# Base10ToBase36 and Base36ToBase10 conversion routines? TIA
 
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 said:
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 said:
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*tokens.IndexOf(tb[tb.Length-i-1]);
}
return ival;
}
}
}

William Stacey said:
Anyone have a c# Base10ToBase36 and Base36ToBase10 conversion
routines?
TIA
 
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 said:
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 said:
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 said:
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*tokens.IndexOf(tb[tb.Length-i-1]);
}
return ival;
}
}
}

Anyone have a c# Base10ToBase36 and Base36ToBase10 conversion routines?
TIA


 
Apparentyly it whacked the link...

http://weblogs.asp.net/justin_rogers/articles/253641.aspx


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers


Justin Rogers said:
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 said:
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 said:
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

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*tokens.IndexOf(tb[tb.Length-i-1]);
}
return ival;
}
}
}

Anyone have a c# Base10ToBase36 and Base36ToBase10 conversion routines?
TIA


 
Justin Rogers said:

nice - but the code i posted works, begs for a bit of optimization, but
comes with *no* strings attached.

rlf


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers


Justin Rogers said:
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 said:
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


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

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*tokens.IndexOf(tb[tb.Length-i-1]);
}
return ival;
}
}
}

Anyone have a c# Base10ToBase36 and Base36ToBase10 conversion
routines?
TIA


 
Ah, I wasn't aware alerting the author about changes to the code
that you've made is a string being attached. The licensing agreement
at the top is primarily a joke for all those that read it. It even requires
that you laugh... I put it there to reduce my own liability and to point
out that the software isn't supported. Take it for what you will.


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

Roy Fine said:
Justin Rogers said:

nice - but the code i posted works, begs for a bit of optimization, but
comes with *no* strings attached.

rlf


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers


Justin Rogers said:
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

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


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

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*tokens.IndexOf(tb[tb.Length-i-1]);
}
return ival;
}
}
}

Anyone have a c# Base10ToBase36 and Base36ToBase10 conversion
routines?
TIA


 
Justin Rogers said:
Ah, I wasn't aware alerting the author about changes to the code
that you've made is a string being attached. The licensing agreement
at the top is primarily a joke for all those that read it. It even requires
that you laugh... I put it there to reduce my own liability and to point
out that the software isn't supported. Take it for what you will.

what the "recommended interpretation" of :

<quote>
The use of this software is for test and performance purposes only.

<\quote>

and

<quote>
In all seriousness,
excluding the laughter, laughter in itself does not void this license
agreement, nor compromise it's ability to legally bind you.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

Roy Fine said:
Justin Rogers said:

nice - but the code i posted works, begs for a bit of optimization, but
comes with *no* strings attached.

rlf


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers


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

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


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

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*tokens.IndexOf(tb[tb.Length-i-1]);
}
return ival;
}
}
}

Anyone have a c# Base10ToBase36 and Base36ToBase10 conversion
routines?
TIA


 
Thanks Roy. Could you expand the range by also including lower case a-z in
addition to uppercase A-Z?
If so, could you spoon feed me again with the updated logic if possible.
What I am looking to do is:
1) 5 base chars (0..9, a...z, A..Z) for count - max range up to
long.MaxRange if possible. Right now max range of ZZZZZ is 60,466,175.
2) Take a hash of count + some secret string(s) using PasswordDeriveBytes
and convert as many bytes as possible to an alpha base encoding for a max of
"HHHH-HHHH-HHHC-CCCC" (five Count positions and 11 Hash positions) to get a
Product key (e.g. MSs). I should be able to recalc the hash at the client
using stripped out count and the shared secret to verify the calculated hash
matches the hash in the Product Key supplied. May need to break the hash
bytes into two longs (16 bytes) and maybe clear high order byte before the
conversion to long so I can pass each long to the BaseXX converter to get
the string. Right now I can do 7 bytes and be sure to stay in
4738381338321616895 range.

I realize the secret is vulnerable, but think I can make it good enouph for
my needs as just an activation code. Anyway, hope above makes some sense.
Basically looking to encode bigger ranges (max longs or max ulongs, or
doubles if possible) with as few chars as possible in the valid set. Many
thanks again. Cheers.

--
William Stacey, MVP
http://mvp.support.microsoft.com

Roy Fine said:
Justin Rogers said:
Ah, I wasn't aware alerting the author about changes to the code
that you've made is a string being attached. The licensing agreement
at the top is primarily a joke for all those that read it. It even requires
that you laugh... I put it there to reduce my own liability and to point
out that the software isn't supported. Take it for what you will.

what the "recommended interpretation" of :

<quote>
The use of this software is for test and performance purposes only.

<\quote>

and

<quote>
In all seriousness,
excluding the laughter, laughter in itself does not void this license
agreement, nor compromise it's ability to legally bind you.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

Roy Fine said:
Apparentyly it whacked the link...

http://weblogs.asp.net/justin_rogers/articles/253641.aspx



nice - but the code i posted works, begs for a bit of optimization, but
comes with *no* strings attached.

rlf



--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers


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

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* 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*tokens.IndexOf(tb[tb.Length-i-1]);
}
return ival;
}
}
}

Anyone have a c# Base10ToBase36 and Base36ToBase10 conversion
routines?
TIA


 
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 said:
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 said:
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 said:
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

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*tokens.IndexOf(tb[tb.Length-i-1]);
}
return ival;
}
}
}

Anyone have a c# Base10ToBase36 and Base36ToBase10 conversion routines?
TIA


 
The use of this software is for test and performance purposes only.

<\quote>

That is to prevent users from holding me liable for putting the code
directly into a production system and having it fail. Because I don't
mention derived works (and this is from my lawyer, I figured I'd
give him a call just to make sure) you could put any derived works
into a production system.
<quote>
In all seriousness,
excluding the laughter, laughter in itself does not void this license
agreement, nor compromise it's ability to legally bind you.
<\quote>

Lawyer just laughed and noted that such jargon would not make this
any more legally binding than me walking up to you on the street and
telling you that I'd copyrighted your name and you were no longer
allowed to be called Roy. Sorry you took it out of scope.
 
William,

With your new base of 26+26+10=62, the max range for 5 digits is now 26^5 -
1, or 916,132,831.

max long is 9,223,372,036,854,775,807. Seems that what you are looking for
is 5 digits of some base that get close to max long. In other words, x^5 =
y, where y = 9,223,372,036,854,775,807.
Solving for x, we get ln(x) = ln(9,223,372,036,854,775,807) / 5, or
x = e^(ln(9,223,372,036,854,775,807) / 5),
or x = 6,208. Base 6208 is not easily representable with our standard
VT-100 vintage keyboards :(

I can find roughly 84 usable characters on my keyboard - the biggest number
that I can express in Base78 is 4,182,119,423. Here is the corresponding
code:

static string tokens =
"0123456789!@#$%^&*()_+=-<>?/.,:;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQ
RSTUVWXYZ";
static long [] powers =
{1L,84L,84L*84L,84L*84L*84L,84L*84L*84L*84L,84L*84L*84L*84L*84L,84L*84L*84L*
84L*84L*84L,84L*84L*84L*84L*84L*84L*84L,84L*84L*84L*84L*84L*84L*84L*84L,84L*
84L*84L*84L*84L*84L*84L*84L*84L};

But - I don't think you want all those silly characters in the product key.

If you want to map 5 characters to a number in the range of
(0..long.MaxValue), consider using base 36 (0..9,A..Z) to represent a seed
value, then use that seed in a simple random number generator (of linear
congruential type). In its simplest form, the expansion could be nothing
more than:
ulong sval = x*84589UL+45989UL;

where x is the value from the 5 Base36 characters, and sval is some really
big number:

/* ***************************** */
string tstr = "ZZZZZ";
ulong x = CONVERSION.BASE.ToLong(tstr);
ulong sval = x*84589UL+45989UL;
Console.WriteLine("real big value: {0}",sval);
/* ***************************** */

As you quickly see, this is a one way mapping - i.e. not a symmetrical
process.

If you want more control over the generation - rather you want a bit more
randomness that is not as easily recognizable, consider a 28 or 32 bit
linear feedback shift register (if 32, then consider taps at 32,7,5,3,2,1
and 0). Load the value from the base 36 representation as the initial
condition, and then shift out as much precision as you need (64 shifts gives
you the range of 0..long.MaxValue).

I don't know if this helps - but it is an interesting exercise.

regards
roy fine


William Stacey said:
Thanks Roy. Could you expand the range by also including lower case a-z in
addition to uppercase A-Z?
If so, could you spoon feed me again with the updated logic if possible.
What I am looking to do is:
1) 5 base chars (0..9, a...z, A..Z) for count - max range up to
long.MaxRange if possible. Right now max range of ZZZZZ is 60,466,175.
2) Take a hash of count + some secret string(s) using PasswordDeriveBytes
and convert as many bytes as possible to an alpha base encoding for a max of
"HHHH-HHHH-HHHC-CCCC" (five Count positions and 11 Hash positions) to get a
Product key (e.g. MSs). I should be able to recalc the hash at the client
using stripped out count and the shared secret to verify the calculated hash
matches the hash in the Product Key supplied. May need to break the hash
bytes into two longs (16 bytes) and maybe clear high order byte before the
conversion to long so I can pass each long to the BaseXX converter to get
the string. Right now I can do 7 bytes and be sure to stay in
4738381338321616895 range.

I realize the secret is vulnerable, but think I can make it good enouph for
my needs as just an activation code. Anyway, hope above makes some sense.
Basically looking to encode bigger ranges (max longs or max ulongs, or
doubles if possible) with as few chars as possible in the valid set. Many
thanks again. Cheers.

--
William Stacey, MVP
http://mvp.support.microsoft.com

Roy Fine said:
Justin Rogers said:
Ah, I wasn't aware alerting the author about changes to the code
that you've made is a string being attached. The licensing agreement
at the top is primarily a joke for all those that read it. It even requires
that you laugh... I put it there to reduce my own liability and to point
out that the software isn't supported. Take it for what you will.

what the "recommended interpretation" of :

<quote>
The use of this software is for test and performance purposes only.

<\quote>

and

<quote>
In all seriousness,
excluding the laughter, laughter in itself does not void this license
agreement, nor compromise it's ability to legally bind you.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers


Apparentyly it whacked the link...

http://weblogs.asp.net/justin_rogers/articles/253641.aspx



nice - but the code i posted works, begs for a bit of optimization, but
comes with *no* strings attached.

rlf



--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers


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

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


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

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*tokens.IndexOf(tb[tb.Length-i-1]);
}
return ival;
}
}
}

Anyone have a c# Base10ToBase36 and Base36ToBase10 conversion
routines?
TIA


 
Lawyer just laughed and noted that such jargon would not make this
any more legally binding than me walking up to you on the street and
telling you that I'd copyrighted your name and you were no longer
allowed to be called Roy. Sorry you took it out of scope.

And I was just getting used to being called Roy... :)
 
I'll go ahead and add some additional types. That is a a fairly easy process. I
need to put some thought into the byte array. Base64 encoding uses a special
padding character to overcome some of the issues you are noting below.

Version 1.1 is posted at the space with all of the base types added.

--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

William Stacey said:
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 said:
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 said:
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


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

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*tokens.IndexOf(tb[tb.Length-i-1]);
}
return ival;
}
}
}

Anyone have a c# Base10ToBase36 and Base36ToBase10 conversion
routines?
TIA


 
Back
Top