Click here to Skip to main content
15,879,490 members
Please Sign up or sign in to vote.
2.00/5 (6 votes)
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
Updated 13-Aug-12 5:30am
v2
Comments
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???
[no name] 13-Aug-12 11:41am    
You will learn a lot more by doing your own homework yourself.

C#
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.
 
Share this answer
 
v2
Comments
riteshsingh_jsr 14-Aug-12 0:16am    
Thanks Clifford... I will try your code...
BillW33 14-Aug-12 9:29am    
Good answer, you get a 5 :)
How about using Math.DivRem(...)?
E.g.
C#
public static string ToExcelName(int num)
{
    List<char> digits = new List<char>();
    do
    {
        int rem;
        num = Math.DivRem(num, 26, out rem);
        digits.Add(Convert.ToChar(65+rem));
    } while (num != 0);
    return string.Join(null, digits.Reverse<char>());
}


Cheers
Andi
 
Share this answer
 
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...
 
Share this answer
 
Comments
riteshsingh_jsr 13-Aug-12 11:52am    
I will try... Thanks for reply...
[no name] 13-Aug-12 12:42pm    
np...
[no name] 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.
[no name] 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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900