Click here to Skip to main content
13,145,774 members (62,654 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I want c# code (function) for bellow formats.
Where A=1, B=2, C=3, D=4, ......Z=26
AA= 27, AB=28,AC=29...... AZ=52
BA=53, BB=54,BC=55, ......BZ=78
And so on...

print output like.

Alphabet A, Number is 1
Alhphabet B, Number is 2

And so on...
Posted 13-Aug-12 5:29am
Updated 13-Aug-12 5:30am
Richard MacCutchan 13-Aug-12 11:32am
This looks like a homework question; I suggest you go back to your notes and review some of the basics. Hint: try doing this with pencil and paper and work it out from there.
Kenneth Haugland 13-Aug-12 11:41am
or excel work :)
riteshsingh_jsr 13-Aug-12 11:51am
Yes, this question asked to me in an interview...
AshishChaudha 13-Aug-12 11:40am
have you tried anything???
Wes Aday 13-Aug-12 11:41am
You will learn a lot more by doing your own homework yourself.
riteshsingh_jsr 13-Aug-12 11:52am
ok... thanks...
CIDev 13-Aug-12 12:28pm
Try writing some code for this then if you have a problem come back with a specific question.
Clifford Nelson 13-Aug-12 13:32pm
This is acutally a hard question if one does it algorithmically. Sort of base 27, but sort of not.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

public static string ConvertAlpha(int value)
    const int a = (int)'A';
    value = value - 1;
    var returnValue = new StringBuilder();
    while (value > -1)
        var remainder = value % 26;
        returnValue.Insert(0, (char)(a + remainder));
        value = value / 26  - 1;
    return returnValue.ToString();
public static int ConvertNumber(string value)
    const int a = (int)'A' - 1;
    int returnValue = 0;
    foreach (var character in value.ToUpper())
        returnValue *= 26;
        returnValue += (int)character - a;
    return returnValue;

Seems to work right.
riteshsingh_jsr 14-Aug-12 0:16am
Thanks Clifford... I will try your code...
CIDev 14-Aug-12 9:29am
Good answer, you get a 5 :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

you can treat it as base 26 format and depending on the position of the alphabet you can use 26*alphabet at 2nd position+ alphabet at 1nd position and so on...
riteshsingh_jsr 13-Aug-12 11:52am
I will try... Thanks for reply...
learner'sbug 13-Aug-12 12:42pm
learner'sbug 13-Aug-12 12:42pm
who is voting it down and why ??
Clifford Nelson 13-Aug-12 15:29pm
I voted it down because it is wrong. It is not a straight base 26 because there is also the blank. You did not test it. If you can create a example that uses straight base 26 I will correct my vote. Wish it was so simple. Also, did not provide the code. You shot from the hip, and missed by a mile. Next time spend a little time testing your concept before posting.
learner'sbug 13-Aug-12 16:02pm
There is no mention of blank in the question.its from A-Z and therefore base 26.and Is it mandatory to have code in the answer ? I don't think so.
Clifford Nelson 13-Aug-12 16:35pm
So I did not explain it so well. Best thing is for you to try your code. Do some work. that will prove it. If you can post code that works with simple base 26, as I said (and you ignored), I will give you a five.

PS. My code will work with three letters, and I posted both the coversion to and from.
Clifford Nelson 13-Aug-12 17:14pm
Since obviously somebody has also been mislead (voted you a 5 when you are wrong and don't know it), I will explain better. In a base system you have a 0 and however many numbers as you specify. If A is zero, then you would have a never ending set of A's when you have any mod. So A is one. Then what is AA. You would think obviously 27, but it is not since Z is 26 and then A? (where ? is the zero value would be 27, and AA should be 28. If you take A as zero instead, then after Z would increment to BA (1 = B, 0 = A), that's not right either.
learner'sbug 14-Aug-12 9:19am
If he want A to be one, why are you talking about 0 based base system. A base system act like how you define it. I f A is 1 and Z is 26 then next number would be AA not A0 because 0 is not define in our base 26 AA would be 1*26^1+1=27 not 28...
Clifford Nelson 14-Aug-12 11:14am
Have you tried coding your solution yet????
learner'sbug 14-Aug-12 13:02pm
private static int ConvertToBase26(string input)
const int i = (int)'A';
int returnInt = 0;
foreach (Char c in input)
returnInt *= 26;
returnInt += (int)c - i+1;

return returnInt;
Clifford Nelson 14-Aug-12 14:09pm
Now try to solve the question instead of convertion back. That is basically the same code I showed above except one minor difference. He did not want the converton from a string to a number, he wanted a number to a string. That one is harder.
learner'sbug 14-Aug-12 14:25pm
"He did not want the converton from a string to a number, he wanted a number to a string.",He just wanted to print Alphabet A, Number is 1
Alhphabet B, Number is can do it using any method ..either using string to number or from number to string..and also my code prints AA = 27..that is what was asked in the question...My answer was clearly telling him to do what was required as per the don't know why do you keep saying that it is base 27.
Clifford Nelson 14-Aug-12 17:29pm
OK gave you a 5 as promised
Rate this: bad
Please Sign up or sign in to vote.

Solution 5

How about using Math.DivRem(...)?
public static string ToExcelName(int num)
    List<char> digits = new List<char>();
        int rem;
        num = Math.DivRem(num, 26, out rem);
    } while (num != 0);
    return string.Join(null, digits.Reverse<char>());


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170915.1 | Last Updated 15 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100