tags:

views:

192

answers:

6

I am trying to convert an index of 1 to 27 into the corresponding uppercase letter. I know that in C++ I could type this:

char letter = 'A' + (char)(myIndex % 27);

This same code does not work in C#. How can I accomplish this task in C#?

EDIT: I'd rather not have to encode an enum or switch statement for this if there is a better mathematical solution like the one above.

+3  A: 
char letter = (char)('A' + (myIndex-1)%26);

(edit out magic number, adjusted indices)

Vlad
Sorry, -1 for magic numbers
Matti Virkkunen
Oh, right. '%26'. Thanks. I guess I was making that one harder than it needed to be.
Mike Webb
+2  A: 

In C#, you'll have to do your casting slightly differently:

char letter = (char)('A' + (myIndex % 27));

However, your math might also be wrong, and this is probably closer to what you actually want:

char letter = (char)('A' + ((myIndex - 1) % 26));
Matti Virkkunen
%27 is wrong, should be 26 :-) There are only 26 letters in Latin alphabet.
Vlad
I kind of copied that from the original code... he says he has an index from 1 to 27 anyways. Of course, to get the correct latin letter he should do ('A' + (char)((myIndex - 1) % 26))
Matti Virkkunen
Indeed, I see. With 26 should work better.
Vlad
Why not correct the answer?
Vlad
Because I'm not quite sure what OP wants exactly. At least this solves the problem with casting.
Matti Virkkunen
+5  A: 

When you add (or subtract) two characters, you get an Int32 in C#. This will work:

int letter = 'A' + (char)(myIndex % 27);

If you want a char, you need to explicitly cast it again:

char letter = (char) ('A' + (char)(myIndex % 27));

However, this most likely should actually be:

char letter = (char) ('A' + (char)((myIndex - 1) % 26));
Reed Copsey
you should s/27/26/g
Vlad
Yes, but I wanted to do the equivelent of the OP's code.
Reed Copsey
@Vlad: You like that better?
Reed Copsey
Yes. Even better would be s/myIndex/(myIndex - 1)/ :-)
Vlad
A: 

This should work...

byte upperA = 65;
byte index = 1;

char letter = (char)(upperA + (index % 27));

Console.WriteLine(letter);

I also like Reed's answer.

Krisc
+3  A: 

Here is a table driven solution:

char ToUpperChar(int index)
{
    if (index < 1 || index > 26)
        throw new ArgumentOutOfRangeException("index");

    return "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[index-1]; // took out the % - it's range-checked above.
}
plinth
+1 for a more original approach. so many type-castings were getting boring ;) And it's purely mathematical since array indexing is syntax sugar for pointer arithmetics.
herenvardo
A: 

How about an extension method?

    public static int Index(this char letter)
    {
        const int offset = 64;
        if(letter >= 'A' && letter <= 'Z')
            return (Convert.ToInt32(letter) - offset);
        return 0;
    }

//usage... char letter = 'A'; int index = letter.Index();

soulia
Sorry! Wrong way. Try this... public static char CharToIndex(this int number) { const int offset = 64; if ( number >= 1 return '\0'; } // Usage ... int i=1; char c = i.CharToIndex();
soulia