Interesting approach wysota. Will try that in the coming days. Meanwhile I found some bugs in the above code I had posted which is mainly due to floating point precision issues that causes a variable of type double with a value of (approximately) three to be turned into two when being casted to an int. Well, you guys probably all have read http://docs.sun.com/source/806-3568/ncg_goldberg.html but I hadn't so I wasted hours debugging this mysterious problem that turned up about 30 times for 500000 inputs.

Here is the updated version which takes an alphabet as a parameter in form of a QList<QChar>. It's still an unsatisfying solution for something like persian where letters are being joined and change their shape when put into a string. Thus, how to handle the resulting string (accessing the individual chars and render them separately or inserting whitespaces between the chars) is the job of the user of this function. Anyway, maybe it turns out to be useful to someone :-).

Qt Code:
  1. QString getLabelFromInt(int index, QList<QChar>& alphabet, int base)
  2. {
  3. // Alphabet should have at least two elements
  4. if (alphabet.size() < 2)
  5. return QString();
  6.  
  7. if (base < 2 || base > alphabet.size())
  8. base = alphabet.size();
  9.  
  10. double tmp = log10((index + 1) * (base - 1) + 1) / log10(base);
  11. int n = (int)floor(tmp + sqrt(numeric_limits<double>::epsilon()));
  12. index -= ((pow(base, n) - 1) / (base - 1)) - 1;
  13.  
  14. QString result(n, alphabet[0]);
  15. int quotient = index;
  16. int strIndex = result.length();
  17. do {
  18. result[--strIndex] = alphabet[quotient % base];
  19. quotient = quotient / base;
  20. } while (quotient != 0);
  21.  
  22. return result;
  23. }
To copy to clipboard, switch view to plain text mode