Answer the question
In order to leave comments, you need to log in
How to implement the letter number by number method?
There is a task:
public static int chars2digits(String number) {
int res = 0;
for (int i = 0, j = number.length() - 1; i < number.length(); i++, j--) {
res += (number.charAt(j)-64)*Math.pow(26, i);
}
return res;
}
Answer the question
In order to leave comments, you need to log in
Here the algorithm is similar to the translation of a number from the N-th system of calculus to the M-th.
Here N = 10, and M = 26
For example, the input number is 153
b = 0
a = 153
Step 1. (last digit of the number)
b = mod(a/26) = 23 (153/26) - the remainder of the division
a = div(a/26) = 5 (153/26) - the integer part of
23 is the character with the code 23 + 64 = W
Step 2. (pre-last digit of the number)
b = mod(a/26) = 5 (5/ 26) - remainder of division
a = div(a/26) = 0 (5/26) - integer part
5 is a character with code 23 + 5 = E
Step 3. (before-before-last digit of the number)
a = 0 - exit
As a result, the number of EW.
Here is my current code in "C with crosses". Indexes - WARNING - start from zero. Will you be able to translate into Java, and leave only letters from the second? The first is valid for any length, the second - for the length of up to 3 characters inherent in Excel'yu.
std::wstring xl::colNameW(
size_t aIndex)
{
wchar_t q[21]; // more than enough even for 64-bit system :)
wchar_t* ptr = q + 20;
*ptr = 0; // debug
// Getting size
size_t n = 1;
unsigned long long pw = 26;
while (aIndex>=pw && n<20)
{
aIndex -= static_cast<size_t>(pw);
pw *= 26;
++n;
}
FOR_S(i, 0, n) // макрос, означающий for (size_t i = 0; i < n; ++i)
{
*(--ptr) = static_cast<wchar_t>(L'A' + (aIndex % 26));
aIndex /= 26;
}
return std::wstring(ptr, n);
}
namespace {
bool isCap(const char* x, const char* aEnd)
{
return (x != aEnd && *x >= 'A' && *x <= 'Z');
}
bool isDig(const char* x, const char* aEnd)
{
return (x != aEnd && *x >= '0' && *x <= '9');
}
} // anon namespace
xl::CellCoords xl::parseCellCoords(
const char* aStart, const char* aEnd)
{
enum {
AA = 26,
AAA = 26 + 26 * 26
};
xl::CellCoords r;
// Letter
r.col = 0;
if (!isCap(aStart, aEnd))
return CellCoords::bad();
r.col = *(aStart++) - 'A';
if (isCap(aStart, aEnd)) {
r.col = (r.col * 26) + *(aStart++) - 'A';
if (isCap(aStart, aEnd)) {
r.col = AAA + (r.col * 26) + *(aStart++) - 'A';
} else {
r.col += AA;
}
}
// Number
r.row = 0;
while (isDig(aStart, aEnd)) {
size_t r0 = r.row;
r.row = r.row * 10 + *(aStart++) - '0';
if (r.row < r0)
return CellCoords::bad();
}
if (r.row == 0 || aStart != aEnd)
return CellCoords::bad();
--r.row;
return r;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question