Here is an improved version if anyone ever happens to need that code. If it shouldn't work for whatever reason use the code I posted first:
QString getLabelFromInt
(int index
) const {
int base = 26;
int n = ((log10((index + 1) * (base - 1) + 1)) / log10(base));
qMin(n, 1);
index -= ((pow(base, n) - 1) / (base - 1)) - 1;
int quotient = index;
QList<int> intList;
do {
intList.prepend(quotient % base);
quotient = quotient / base;
} while (quotient != 0);
for (unsigned i = 0; i < n - intList.size(); ++i)
str += 'a';
QList<int>::const_iterator iter;
for (iter = intList.begin(); iter != intList.end(); ++iter)
str += *iter + 'a';
return str;
}
QString getLabelFromInt(int index) const
{
int base = 26;
int n = ((log10((index + 1) * (base - 1) + 1)) / log10(base));
qMin(n, 1);
index -= ((pow(base, n) - 1) / (base - 1)) - 1;
int quotient = index;
QList<int> intList;
do {
intList.prepend(quotient % base);
quotient = quotient / base;
} while (quotient != 0);
QString str;
for (unsigned i = 0; i < n - intList.size(); ++i)
str += 'a';
QList<int>::const_iterator iter;
for (iter = intList.begin(); iter != intList.end(); ++iter)
str += *iter + 'a';
return str;
}
To copy to clipboard, switch view to plain text mode
and here is the C++ version without Qt
string getLabelFromInt(int index)
{
int base = 26;
int n = ((log10((index + 1) * (base - 1) + 1)) / log10(base));
n = (n < 1) ? 1 : n;
index -= ((pow(base, n) - 1) / (base - 1)) - 1;
int quotient = index;
list<int> intList;
do {
intList.push_front(quotient % base);
quotient = quotient / base;
} while (quotient != 0);
string str;
for (unsigned i = 0; i < n - intList.size(); ++i)
str += 'a';
list<int>::const_iterator iter;
for (iter = intList.begin(); iter != intList.end(); ++iter)
str += *iter + 'a';
return str;
}
string getLabelFromInt(int index)
{
int base = 26;
int n = ((log10((index + 1) * (base - 1) + 1)) / log10(base));
n = (n < 1) ? 1 : n;
index -= ((pow(base, n) - 1) / (base - 1)) - 1;
int quotient = index;
list<int> intList;
do {
intList.push_front(quotient % base);
quotient = quotient / base;
} while (quotient != 0);
string str;
for (unsigned i = 0; i < n - intList.size(); ++i)
str += 'a';
list<int>::const_iterator iter;
for (iter = intList.begin(); iter != intList.end(); ++iter)
str += *iter + 'a';
return str;
}
To copy to clipboard, switch view to plain text mode
Bookmarks