c - Why does this code work to convert hexadecimal to decimal -
this code convert 1 hexadecimal digit decimal value. int value; // ch char variable holding hexadecimal digit if (isxdigit(ch)) if (isdigit(ch)) value = ch - '0'; else value = tolower(ch) - 'a' + 10; else fprintf(stderr, "%c not valid hex digit", ch); i don't understand how works though. can see different things subtracted char variable depending on whether number or letter. can understand part number gets converted, don't understand why 10 has added value when character letter. the subtraction of tolower(ch) - 'a' map character number in range 0..5 letters a..f. however, (decimal) value of hexadecimal digit a 16 10 10 , move range 10..15 needs be, 10 added. perhaps helps: +---------+------------+-----------------+-------------+ character | subtracted | resulting value | digit value | +---------+------------+-----------------+-------------+ | '0' | '0' | 0 ...