2

I'm trying to duplicate the way excel provides labels its columns where

A = 1
B = 2

so on and so forth, so that it eventually reaches

AB
AC
AD

etc, etc.

How do I algorithmically take a number (like 52) and convert it to its equivalent alphabet representation?

brettdj
  • 54,857
  • 16
  • 114
  • 177
meds
  • 21,699
  • 37
  • 163
  • 314
  • 1
    I do not see why someone should vote to close it? If the question is not clear to one, one may ask for clarification. – Kangkan Dec 29 '11 at 10:02
  • 1
    no, its good question +1 – Yola Dec 29 '11 at 10:03
  • 1
    People vote to close only because it isn't exactly formulated as a question. Doh... – Kos Dec 29 '11 at 10:05
  • 1
    @meds I've rephrased the question a little bit so that the SO lawyers would lose their reason to cast close votes on this question. Cut it out, folks... – Kos Dec 29 '11 at 10:33
  • 1
    There are many questions asking this for different languages. Eg see [this answer](http://stackoverflow.com/q/667802/445425), asked for C# – chris neilsen Dec 29 '11 at 10:48

8 Answers8

2
string get(int a) {
  a--; // Reduce by one to make the values with 1 letter 0..25, 
       // with two - 26.. 26^2-1 and so on
  int val = 0; // number of columns with no more then given number of letters
  int number = 0;
  while (val < a) {
    val = val*26 + 26;
    number++;
  }
  val = (val - 26)/26;
  a -= val; // subtract the number of columns with less letters 
  string res;
  for (int i = 0; i < number; ++i) {
    res.push_back(a%26 + 'A');
    a /= 26;
  }
  reverse(res.begin(), res.end());
  return res;
}

Hope that helps.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
2

for two letters

#include <iostream>
#include <string>

using namespace std;

string int2alphas(int i) {
    string res;
    if (i > 26 - 1)
        res.push_back(i / 26 + 'A' - 1);
    else 
        res.push_back(' ');
    res.push_back(i % 26 + 'A');
    return res;
}

void test(int t) {
    cout << t << "-" << int2alphas(t) << endl;;
}


int main() {
    for (int i = 0; i < 55; i++)
        test(i);
}
Yola
  • 18,496
  • 11
  • 65
  • 106
1

Convert.ToInt32("52", 26).... And now just create the correct base implementation. ? homework ?

tryme
  • 81
  • 3
1

You may think of writing some algo like:

ConvertToAlphaCode(number input)
{
    Array Chars=[A-Z]
    if (number<= 26)
        return Chars[number-1]
    else
        ...
}
Kangkan
  • 15,267
  • 10
  • 70
  • 113
1

Have a look:

A, B, C, D, ..., Y, Z, AA, AB, AC, AD, ..., AY, AZ, BA, BB, ...

is exactly like:

0, 1, 2, 3, 4, ..., 9, 10, 11, 12, 13, ..., 19, 20, 21, ...

but with digits A..Z instead of 0..9. So:

Algorithmic-ally I'm not sure how I can get a number, like say 52, and convert it to the equivalent alphabet representation..

You need to use a generic algorithm to convert a number in base-N to base-M (like decimal to hexadecimal), but with N equal to 10 and M equal to 26 (letters), and make sure that you use correct characters to represent the final "digits". As simple as that!

Kos
  • 70,399
  • 25
  • 169
  • 233
1

This will do it quite well:

string calcString(int number)
{
    string returnValue = "";
    do
    {
        int rm = number % 26;
        returnValue += (char)(rm+ 'A');
        number = (number - rm) / 26;
   } 
   while (number > 0);

   return returnValue;
}

For example, calcString(11); results in L.

If this is not precisely the calculation for which you were looking, leave a comment to clarify what you want and I'll come back to change it.

Zéychin
  • 4,135
  • 2
  • 28
  • 27
  • Note that the encoding of letters is not guaranteed to be contiguous (although except for IBM mainframes, you're unlikely to encounter an encoding where the 26 unaccented letters aren't contiguous). – James Kanze Dec 29 '11 at 17:48
0

This will work with all types of letters (small, big).

using namespace std;

int lettervalue (string l) {
    l=l[0];
    string alpha="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    int idx = alpha.find(l)+1;
    if(idx>26){
        idx=idx-26;
    }

    return idx;

}

To use it:

cout << lattervalue("e"); //will return 5(int)
Jonathan Gurebo
  • 1,089
  • 1
  • 11
  • 21
0

From number to letter:

static std::string const letters( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
assert( n >= 0 && n < letters.size() );
return letters[n];

From letter to number:

static std::string const letters( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
char const* result = std::find(
            letters.begin(),
            letters.end(),
            isupper( static_cast< unsigned char >( l ) );
assert( result != letters.end() );
return result - letters.begin();

EDIT:

This just handles a single character in each direction. For more, it's just a base 26 conversion, using the usual conversion routine.

James Kanze
  • 150,581
  • 18
  • 184
  • 329