views:

140

answers:

6

What is the best way of converting a multi-digit number to a alphabet string in C#.

For example, if I have a number say,

int digits = 1234567890

I want this to be converted to a string which would be

string alpha = "ABCDEFGHIJ"

This means 1 represents A, 2 represents B and so on.

A: 

Make an array of chars:

char alphabet = {'A','B','C' .... }

Then use the digit number as the index of the array:

digit = 14567
char[] digit_ar = new String(digit).ToCharArray();

foreach (char c in digit_ar)
{
    string s+=digit_ar[Convert.ToInt32(c)-Base];
}

Base is the A char code

This is a pseudo code and I've not tested it!

It should works...

robob
Wouldn't that be a little bit overkill for simple Unicode arithmetic?
Pieter
I wrote this is pseudo code...and not so efficient. The Idea is The important thing.
robob
We wrote the same thing and the difference is only in the constant factors of the algorithm...I think your -1 to my post is wrong. You write "output = (char)((char)'A' + (current - 1)) + output;" but this is wrong in OO because every time it makes a new object. So if you number is 10 digits it creates 10 object in memory...Use StringBuilder instead...I cannot assign you -1...
robob
A: 

Use Dictionary class, look at this.

cevik
Wouldn't that be a little bit overkill for simple Unicode arithmetic?
Pieter
Maybe, but this is great for any further requirements
cevik
+7  A: 

Something like this:

int input = 123450;
string output = "";

while (input > 0)
{
    int current = input % 10;
    input /= 10;

    if (current == 0)
        current = 10;

    output = (char)((char)'A' + (current - 1)) + output;
}

Console.WriteLine(output);

The above saves you the trouble of having to define a conversion list through an array or dictionary. The conversion can be done simply calculating the correct Unicode codepoint.

Pieter
It gives me B instead of A in the beginning.
Wajih
doesn't this output "54321" equivalent?
ytg
Updated the answer. It should now start with A.
Pieter
@ytg No. The modules takes the last number and because of this, the character needs to be put in front. With modules, you start building from the end of the number string up to the front.
Pieter
One last thing, for 0 should give J, its giving me @ symbol right now. The digit combination could nay number of combinations
Wajih
@Wajih: Updated the answer. Current is now set to 10 so it will produce J.
Pieter
@ Pieter. Thanks dude, you're the man!!!
Wajih
You're welcome.
Pieter
"output = (char)((char)'A' + (current - 1)) + output;" concatenation of string done with "+" operator is not a good thing in the algorithm you propose. As you see, your algorithm is a little bit overkilling in term of memory and object allocation..- -1 for me, -1 for you :-)
robob
Fair enough :). The thing though is that when we would use a StringBuilder, we would have to turn the concatenation around and that would make the whole logic a log complexer. Suggestions?
Pieter
A: 

There are not too many different symbols to replace, just a simple replace would be easy.

int digits = 0123456789;
string digitsAsString = digits.ToString("0000000000"); // Trick to preserve the 0.
string alpha = digitsAsString
    .Replace('0', 'A')
    .Replace('1', 'B')
    .Replace('2', 'C')
    .Replace('3', 'D')
    .Replace('4', 'E')
    .Replace('5', 'F')
    .Replace('6', 'G')
    .Replace('7', 'H')
    .Replace('8', 'I')
    .Replace('9', 'J');
Console.WriteLine(alpha);
Albin Sunnanbo
+2  A: 

First of all, 0123 would be interpreted as 123, so the leading 0 would be ignored. Here's one possible solution:

int i = 1230468;
StringBuilder res = new StringBuilder(i.ToString());

for (int j = 0; j < res.Length; j++)
   res[j] += (char)(17); // '0' is 48, 'A' is 65

Console.Out.WriteLine(res.ToString()); // result is BCDAEGI
alex
Almost fine, but gives me B instead of A for 1
Wajih
Still acceptable.
Wajih
A: 

Pay attention to this instruction

"output = (char)((char)'A' + (current - 1)) + output;"

At every iteration it creates new objects. String concatenation must be done with StringBuilder...

I have to do a general consideration to the answers given to this post. A developer must pay attention not only to the elegance, or algorithm efficiency but to memory efficiency too.

C# is not C o C++ and Objects are bad animals :-)

robob