473,386 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Character Value with Base 26 with C#

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
Nov 15 '05 #1
6 14088
On Thu, 29 Jan 2004 12:03:50 -0500, Tony Tortora wrote:
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 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;
}
-----------
End snippet

HTH,
Tim
--
Tim Smelser - MVP Visual C#
To email me, make the snot hot.
Nov 15 '05 #2
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.
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;
}
-----------
End snippet

HTH,
Tim
--
Tim Smelser - MVP Visual C#
To email me, make the snot hot.

Nov 15 '05 #3
On Thu, 29 Jan 2004 17:01:53 -0500, Gary Morris wrote:
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
--
Tim Smelser - MVP Visual C#
To email me, make the snot hot.
Nov 15 '05 #4
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

"Tony Tortora" <AT@att.com> wrote in message
news:e%****************@TK2MSFTNGP11.phx.gbl...
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

Nov 15 '05 #5
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
Nov 15 '05 #6
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;

}

"Matthew W. Jackson" <th********************@NOSPAM.NOSPAM> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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

Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: kamp | last post by:
Hello, Below is a snippet from a schema. The second enumeration should contain an i umlaut (archaļsch) but when I use this schema with Altova's Stylevision software the iumlaut is not displayed...
9
by: Erik Leunissen | last post by:
L.S. I've observed unexpected behaviour regarding the usage of the '#' flag in the conversion specification in the printf() family of functions. Did I detect a bug, or is there something wrong...
6
by: Lucvdv | last post by:
In a sorted list, certain characters are grouped together, for example "ēb" (cedille b) comes after "ca" but before "cc". Is there a built-in way to find out what 'base character', such as C for...
0
by: Phil C. | last post by:
(Cross post from framework.aspnet.security) Hi. I testing some asp.net code that generates a 256 bit Aes Symmetric Key and a 256 bit entropy value. I encrypt the Aes key(without storing it as...
6
by: Claude Henchoz | last post by:
Hi guys I have a huge list of URLs. These URLs all have ASCII codes for special characters, like "%20" for a space or "%21" for an exclamation mark. I've already googled quite some time, but I...
1
by: =?Utf-8?B?UGF1bCBQaGlsbGlwcw==?= | last post by:
I have read many things about this but I haven't got a clear vision on what to do if anything about this. I have a system that tries to find holes in my web site. One of the things it has...
1
by: banagani | last post by:
Hi All, I am facing an issue in the XmlTextWriter class in the dotnet 2.0. This is the sample code Actual XML is like this <Name>&#x8A73;&#x7D30;&#x4ED5;&#x69D8;&#x306B;</Name>
3
by: =?Utf-8?B?Vmlub2Q=?= | last post by:
Hi All, I am facing an issue in the XmlTextWriter class in the dotnet 2.0. This is the sample code Actual XML is like this <Name>č©³ē“°ä»•ę§˜ć«</Name>
11
by: Fred Chateau | last post by:
I found the following code in an online tutorial, and I'm having some difficulty understanding it. Unfortunately, it did not list any expected output examples for val. What type is val? Is this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.