Need an Algorithm
-
I´am working on a project where i have to query some values from a database. I have to convert those values into letters from 'a' to 'z'. example: value: letter 1 -> 'a' 2 -> 'b' 3 -> 'c' ... 27 -> 'aa' 28 -> 'ab' 29 -> 'ac' The letters can go up to 'zzzz' Can anybody help me with this stupid thing? P.
CString str; int number = 53; do { number--; str.Insert(0, 'a' + (number % 26)); number /= 26; } while (number > 0); TRACE(_T("str: %s"), str);
-
I´am working on a project where i have to query some values from a database. I have to convert those values into letters from 'a' to 'z'. example: value: letter 1 -> 'a' 2 -> 'b' 3 -> 'c' ... 27 -> 'aa' 28 -> 'ab' 29 -> 'ac' The letters can go up to 'zzzz' Can anybody help me with this stupid thing? P.
int iVal = 453; // For example
char Result[5] = "";if (iVal >= 17577) { // For the most left character
Result[0] = (iVal % 26) - 1 + 'A';
}
else {
Result[0] = '\0';
}if (iVal >= 677) { // For the second left character
Result[1] = (iVal % 26) - 1 + 'A';
}
else {
Result[1] = '\0';
}if (iVal >= 27) { // For the third left character
Result[2] = (iVal % 26) - 1 + 'A';
}
else {
Result[2] = '\0';
}if (iVal >= 1) { // For the most right character
Result[3] = (iVal % 26) - 1 + 'A';
}
else {
Result[3] = '\0';
}Result[4] = '\0';
// Re-order the string (erases the '\0' at the beginning of the string)...
for (int i = 0, i < 4, i++) {
if (Result[1] == '\0') {
Result[i] = Result[i+1];
}
}i didnot tested however... :cool:
TOXCCT >>> GEII power
[toxcct][VisualCalc] -
you code won't work because you consider that
if (Number>=27)
only... but ifNumber == 53
, the result must be"ba"
, etc... as P-Rex said, there are until"zzzz"
possibilities, that means26^4 = 456976
cases...
TOXCCT >>> GEII power
[toxcct][VisualCalc]I think you didn't understand completely my code ;) For exemple if Number == 53, the result must be "ab" and not "ba". Thus in my code, if (Number >= 27), I DON'T jump out of the loop but comes once again in the loop, thus adding 'b' to the already existing string (that contains 'a'). Is it a little bit clearer ? [EDIT] Sorry, I don't add 'b' but 'z' because 53-27 = 26 that corresponds to 'z' :) [EDIT2] After 2 minutes of reflexion, I see that there effectively a problem in the code :~ . There will only be 'a' in the begining of the string... I must not subtract but rather divide by 26... But I think the other solutions are good
-
CString str; int number = 53; do { number--; str.Insert(0, 'a' + (number % 26)); number /= 26; } while (number > 0); TRACE(_T("str: %s"), str);
-
As he properly realized, going from a number to a 'A' to 'ZZZZ' representation is the same as printing the number base 26 where the digits are the letters 'A' - 'Z'. If you wanted to write a number converter to print that value as decimal, then just change the / 26 to / 10 and the initial character from 'A' to '0'. Tim Smith I'm going to patent thought. I have yet to see any prior art.
-
I´am working on a project where i have to query some values from a database. I have to convert those values into letters from 'a' to 'z'. example: value: letter 1 -> 'a' 2 -> 'b' 3 -> 'c' ... 27 -> 'aa' 28 -> 'ab' 29 -> 'ac' The letters can go up to 'zzzz' Can anybody help me with this stupid thing? P.
What value is represented by '0' ? If you know that, then you have a base 26 system. See, everything goes up to 26, then it carries over one place. You are just mapping a base 26 numeric system to letters.
-
int iVal = 453; // For example
char Result[5] = "";if (iVal >= 17577) { // For the most left character
Result[0] = (iVal % 26) - 1 + 'A';
}
else {
Result[0] = '\0';
}if (iVal >= 677) { // For the second left character
Result[1] = (iVal % 26) - 1 + 'A';
}
else {
Result[1] = '\0';
}if (iVal >= 27) { // For the third left character
Result[2] = (iVal % 26) - 1 + 'A';
}
else {
Result[2] = '\0';
}if (iVal >= 1) { // For the most right character
Result[3] = (iVal % 26) - 1 + 'A';
}
else {
Result[3] = '\0';
}Result[4] = '\0';
// Re-order the string (erases the '\0' at the beginning of the string)...
for (int i = 0, i < 4, i++) {
if (Result[1] == '\0') {
Result[i] = Result[i+1];
}
}i didnot tested however... :cool:
TOXCCT >>> GEII power
[toxcct][VisualCalc]:wtf:
-
:wtf:
-
I think you didn't understand completely my code ;) For exemple if Number == 53, the result must be "ab" and not "ba". Thus in my code, if (Number >= 27), I DON'T jump out of the loop but comes once again in the loop, thus adding 'b' to the already existing string (that contains 'a'). Is it a little bit clearer ? [EDIT] Sorry, I don't add 'b' but 'z' because 53-27 = 26 that corresponds to 'z' :) [EDIT2] After 2 minutes of reflexion, I see that there effectively a problem in the code :~ . There will only be 'a' in the begining of the string... I must not subtract but rather divide by 26... But I think the other solutions are good
You sound like you have it. But in case you haven't thought of this, you can imagine you are working with a base 27 system of numbers and you are converting from decimal. Begin by checking if your value is greater than (27^3+1) if it is get the mod of your input number by (27^3+1 because a is 1), that's your first digit which falls between 0 and 26. then subtract that number off your working total. Then check if the remainder is bigger than (27^2+1). If it is, then mod by (27^2+1), that's your second digit. And so on. Since you are working with a-z just add the value of the char('a') to each mod and save yourself a switch statement. One for loop of four iterations should do it.
-
int iVal = 453; // For example
char Result[5] = "";if (iVal >= 17577) { // For the most left character
Result[0] = (iVal % 26) - 1 + 'A';
}
else {
Result[0] = '\0';
}if (iVal >= 677) { // For the second left character
Result[1] = (iVal % 26) - 1 + 'A';
}
else {
Result[1] = '\0';
}if (iVal >= 27) { // For the third left character
Result[2] = (iVal % 26) - 1 + 'A';
}
else {
Result[2] = '\0';
}if (iVal >= 1) { // For the most right character
Result[3] = (iVal % 26) - 1 + 'A';
}
else {
Result[3] = '\0';
}Result[4] = '\0';
// Re-order the string (erases the '\0' at the beginning of the string)...
for (int i = 0, i < 4, i++) {
if (Result[1] == '\0') {
Result[i] = Result[i+1];
}
}i didnot tested however... :cool:
TOXCCT >>> GEII power
[toxcct][VisualCalc]Dosen't work toxcct