If you have a char that is in the range \'0\' to \'9\' how do you convert it to int values of 0 to 9
And then how do you convert it back?
Also given letters
The basic char encoding specified by C++ makes converting to and from '0' - '9' easy.
C++ specifies:
In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.
This means that, whatever the integral value of '0', the integral value of '1' is '0' + 1, the integral value of '2' is '0' + 2, and so on. Using this information and the basic rules of arithmetic you can convert from char to int and back easily:
char c = ...; // some value in the range '0' - '9'
int int_value = c - '0';
// int_value is in the range 0 - 9
char c2 = '0' + int_value;
Portably converting the letters 'a' to 'z' to numbers from 0 to 25 is not as easy because C++ does not specify that the values of these letters are consecutive. In ASCII they are consecutive, and you can write code that relies on that similar to the above code for '0' - '9'. (These days ASCII is used most everywhere).
Portable code would instead use a lookup table or a specific checks for each character:
char int_to_char[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
int char_to_int[CHAR_MAX + 1] = {};
for (int i=0; i
In C99 you can just directly initialize the char_to_int[] data without a loop.
int char_to_int[] = {['a'] = 0, ['b'] = 1, ['c'] = 2, ['d'] = 3, ['e'] = 4, ['f'] = 5, ['g'] = 6, ['h'] = 7, ['i'] = 8, ['j'] = 9, ['k'] = 10, ['l'] = 11, ['m'] = 12, ['n'] = 13, ['o'] = 14, ['p'] = 15, ['q'] = 16, ['r'] = 17, ['s'] = 18, ['t'] = 19, ['u'] = 20, ['v'] = 21, ['w'] = 22, ['x'] = 23, ['y'] = 24, ['z'] = 25};
C++ compilers that also support C99 may support this in C++ as well, as an extension.
Here's a complete program that generates random values to use in these conversions. It uses C++, plus the C99 designated initialization extension.
#include
int digit_char_to_int(char c) {
assert('0' <= c && c <= '9');
return c - '0';
}
char int_to_digit_char(int i) {
assert(0 <= i && i <= 9);
return '0' + i;
}
int alpha_char_to_int(char c) {
static constexpr int char_to_int[] = {['a'] = 0, ['b'] = 1, ['c'] = 2, ['d'] = 3, ['e'] = 4, ['f'] = 5, ['g'] = 6, ['h'] = 7, ['i'] = 8, ['j'] = 9, ['k'] = 10, ['l'] = 11, ['m'] = 12, ['n'] = 13, ['o'] = 14, ['p'] = 15, ['q'] = 16, ['r'] = 17, ['s'] = 18, ['t'] = 19, ['u'] = 20, ['v'] = 21, ['w'] = 22, ['x'] = 23, ['y'] = 24, ['z'] = 25};
assert(0 <= c && c <= sizeof(char_to_int)/sizeof(*char_to_int));
int i = char_to_int[c];
assert(i != 0 || c == 'a');
return i;
}
char int_to_alpha_char(int i) {
static constexpr char int_to_char[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
assert(0 <= i && i <= 25);
return int_to_char[i];
}
#include
#include
int main() {
std::random_device r;
std::seed_seq seed{r(), r(), r(), r(), r(), r(), r(), r()};
std::mt19937 m(seed);
std::uniform_int_distribution digits{0, 9};
std::uniform_int_distribution letters{0, 25};
for (int i=0; i<20; ++i) {
int a = digits(m);
char b = int_to_digit_char(a);
int c = digit_char_to_int(b);
std::cout << a << " -> '" << b << "' -> " << c << '\n';
}
for (int i=0; i<20; ++i) {
int a = letters(m);
char b = int_to_alpha_char(a);
int c = alpha_char_to_int(b);
std::cout << a << " -> '" << b << "' -> " << c << '\n';
}
}