W
William Stacey [MVP]
Anyone have a c# Base10ToBase36 and Base36ToBase10 conversion routines? TIA
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: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 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
{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*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 =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;
}
}
}
routines?William Stacey said:Anyone have a c# Base10ToBase36 and Base36ToBase10 conversion
TIA
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
{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*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 =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: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:
--
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
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.
--
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
not--
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
gosetdepending 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
of theitnumber set and the weights of each position. For the Base26 case,
was{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*this:
/* ***************** */
public class BASE32{
static string tokens = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static long [] powers =arrays.26L*26L*26L*26L*26L*26L*26L*26L*26L*26L};
...
...
}
/* ***************** */
conversion is each direction is based on the tokens and powers
the{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*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 =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
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.
{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 ---
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 =
1;{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*public class BASE32{
static string tokens = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static long [] powers =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: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
{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*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 =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>
<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>
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
{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*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.
may--
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
nottokengo
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
setcase,of the
number set and the weights of each position. For the Base26
itwas
this:
/* ***************** */
public class BASE32{
static string tokens = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
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*or26L*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
zerocomputedvalue, 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
at4,738,381,338,321,616,896 -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
1;public class BASE32{
static string tokens = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static long [] powers =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.
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