Character Value with Base 26 with C#

  • Thread starter Thread starter Tony Tortora
  • Start date Start date
T

Tony Tortora

I am writing and add on application. The application uses Unique IDs and
they are stored in Base 26 (ie 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ).
I am having trouble reading the decimal value of a character of the key and
to assign a the new value for the Unique ID.

For example to change "A" to "B". Normally, I would read the ASCII value of
"A" which is 65, add one making it 66 which is "B".

How can I do this is C#.

Thanks
 
I am writing and add on application. The application uses Unique IDs and
they are stored in Base 26 (ie 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ).
I am having trouble reading the decimal value of a character of the key and
to assign a the new value for the Unique ID.
For example to change "A" to "B". Normally, I would read the ASCII value of
"A" which is 65, add one making it 66 which is "B".
How can I do this is C#.

I assume that you are trying to convert to and from Base 26 encoding.
Here is some sample code that does that for unsigned integers. If you
prefer lower case, just modify accordingly.

Begin snippet
-------------
static string ConvertToBase26(uint i) {
const int BASE = 26;
StringBuilder result = new StringBuilder();
uint remainder;

while (i > 0) {
remainder = i % BASE;
i = i / BASE;
result.Insert(0, (char)((char)remainder + 'A'));
};

return result.ToString();
}

static uint ConvertFromBase26(string val) {
const double BASE = 26.0;
uint ret = 0;

char[] vals = val.ToUpper().ToCharArray();
int last = vals.Length - 1;

for (int x = 0; x < vals.Length; x++) {
if (vals[x] < 'A' || vals[x] > 'Z')
throw new ArgumentException("Not a valid Base26 string.", val);

ret += (uint)(Math.Pow(BASE, (double)x) * (vals[last - x] - 'A'));
}

return ret;
}
 
That's good, but both of you guys are missing a fundamental point here:
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" is 36 chars, not 26!
With a base of 26, the last usable char will be 'P', not 'Z' as in base 36.

I came up with a routine back in my VB days that allows the one-way
conversion from decimal to base whatever. I converted this to C#, and
I'm sure with a little thought it could be modified to be more efficient,
but I'm feeling lazy right now, so I'll just post what I have now:

public string chBase(int x, int Base)
{
int modx = 0;
string retVal = string.Empty;

if(x == 0)
return "0";
if(x < 0)
return ""; // Error
while(x > 0)
{
modx = x % Base;
if(modx < 10)
retVal = modx.ToString() + retVal;
else
retVal = Convert.ToChar(modx + 55) + retVal;
x = x / Base;
}
return retVal;
}

No fancy error checks or anything, but when I wrote this I knew that I
would always be passing correct values. Even so, I did add the checks
for 0 and < 0.
 
That's good, but both of you guys are missing a fundamental point here:
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" is 36 chars, not 26!
With a base of 26, the last usable char will be 'P', not 'Z' as in base 36.
I came up with a routine back in my VB days that allows the one-way
conversion from decimal to base whatever. I converted this to C#, and
I'm sure with a little thought it could be modified to be more efficient,
but I'm feeling lazy right now, so I'll just post what I have now:
public string chBase(int x, int Base)
{
int modx = 0;
string retVal = string.Empty;
if(x == 0)
return "0";
if(x < 0)
return ""; // Error
while(x > 0)
{
modx = x % Base;
if(modx < 10)
retVal = modx.ToString() + retVal;
else
retVal = Convert.ToChar(modx + 55) + retVal;
x = x / Base;
}
return retVal;
}
No fancy error checks or anything, but when I wrote this I knew that I
would always be passing correct values. Even so, I did add the checks
for 0 and < 0.

Base 26 (in all of the references I have ever found for it) has no
numbers. It is simply using the letters of the alphabet as a numerical
representation. That is why I stipulated my assumption that Base 26 is
actually what Tony wanted (since, as you noted, the character set he
posted was a base 36).

Tim
 
First, that's base 36, I believe.

Second, you can access a string by index and return a character, which can
be cast directly to an iteger.

To get the Unicode (rather than ASCII in .NET) value of the third character
of a string, for example, you would use:

string myString = "SOMESTRING";
int code = (int)myString[2];

Luckily, the basic alphabet has the same code points in Unicode as they do
in ASCII. Still, you may want to do subtraction from the char literal 'A'
to make the code more readable.

int result = 0;
int factor = 1;

for(int index = myString.Length; index >= 0; index--) {

int digitValue = 0;
char digit = myString[index];

if(Char.IsDigit(digit))
{
digitValue = (int)(digit) - (int)('0');
}
else if(Char.IsUpper(digit))
{
digitValue = 10 + (int)(digit) - (int)('A');
}
else if(Char.IsLower(digit))
{
digitValue = 10 + (int)(digit) - (int)('a');
}

result += factor * digitValue;
factor *= 36;

}

// result contains the converted number

I haven't tested that code, so you'll want to try it yourself and possibly
optimize it or make it your own. You might overflow an Int32 pretty quickly
so you may need to use 64-bit math.

--Matthew W. Jackson
 
OOOPS!

On my example, change "myString.Length" to "myString.Length - 1"

I actually don't write too many backwards for loops.

If there had been a reverse foreach, I would have used that. :-)

--Matthew W. Jackson
 
Thanks for all your help. I tried to do a fancy solution. I was having
trouble so I took a simple solution that works.

Here is my solution if you ar interested.

private string AddOneBase36(string MNumber)
{
bool MAddOne;
string MNewKey;
int MCounter;
int MIndex;

string MResult;
string MBase36 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";


MResult = "";
MNewKey = "";
MAddOne = true;

for (MCounter = MNumber.Length-1; MCounter >= 0 ; MCounter--)
{
MIndex = MBase36.IndexOf(MNumber[MCounter]);

if (MAddOne)
{
if (MIndex == MBase36.Length - 1)
{
MIndex = 0;
MAddOne = true;
}
else
{
MIndex++;
MAddOne = false;
}
}

MNewKey = MNewKey + MBase36[MIndex];

}


for (MCounter = MNewKey.Length-1 ; MCounter >= 0; MCounter--)
{
MResult = MResult + MNewKey[MCounter];
}

return MResult;

}
 
Back
Top