What I was trying to explain briefly about
s[ s[i] ]
in my last post, is that it makes no sense. Think about it.
s[i]
is a hex char from a number that you are converting which is then used as an index to the same s array.
s[ char that you are converting ]
just does not make any logical sense. Which is why I recommended another array,
other_array
. If you really did want to use another array to store the values, you would need to initialize the locations '0' to '9' with 0 to 9, 'a' to 'f' and 'A' to 'F' with 10 to 15, and all the other locations with some error value. I can assure you that using another array is a bad road to travel down for this problem. A function handles this same mapping of inputs to outputs in a much more readable way than the code for this other array would.
Not being stuck on a specific numeral system is a bit unrelated to this char to hex digit conversion. 'a' should always have a value of 10
10, 12
8, 1010
2, etc.. Therefore this hex digit conversion function is correct when it reports 10
10 for the input of 'a'. It would also be correct if it reported 12
8. Because 10
10 and 12
8 are equivalent. This kind of conversion is happening very frequently within a computer. All numbers are ultimately represented with binary base in memory. When C++ has to display this binary representation in base 10, somewhere down the line is a function that does the math to figure out what ASCII digits need to be displayed.
In C++ it is possible to convert how integers are displayed to a few different bases.
http://www.cplusplus.com/reference/ios/oct/
It is also possible to specify literal numbers in bases other than 10.
int sixteen = 0x10; //0x is the prefix to use hexadecimal