I’m converting a character string character by character into integers. So ‘A’ – ‘0’ should be 10. However even though the numbers come out fine, alphabetical characters (i.e. A-F) come out as being off by 7. For instance, here’s my line of code for conversion:
result = result + (((int) (*new - '0')) * pow(16, bases));
If I print that line piece by piece for a hex string like “A2C9” then for some reason my A is converted to 17 and my C is converted to 19. However the numbers 2 and 9 come out correctly. I’m trying to figure out if I’m missing something somewhere.
The ASCII code for ‘A’ is 65; for ‘Z’, it is 90.
The ASCII code for ‘0’ is 48; for ‘9’, it is 57. These codes are also used in Unicode (UTF-8), 8859-x, and many other codesets.
When you calculate
'A' - '0', you get 65 – 48 = 17, which is the ‘off-by-seven’ you are seeing.To convert the alphabetic characters ‘A’ to ‘F’ to their hex equivalents, you need some variation on:
Remembering that ‘a’ to ‘f’ are also allowed and for them you’d need:
Or you’d need to convert to upper-case first. Or you can use:
or any of a myriad other techniques. This last fragment assumes that
cis known to contain a valid hex digit. It fails horribly if that is not the case.Note that C does guarantee that the codes for the digits 0-9 are consecutive, but does not guarantee that the codes for the letters A-Z are consecutive. In particular, if the codeset is EBCDIC (mainly but not solely used on IBM mainframes), the codes for the letters are not contiguous.