Why do we use n = 16 * n + hexdigit
Mar 27, 2015
There is a program to convert a string of hexadecimal digits into its equivalent integer value as below.
1 | enum loop {NO, YES}; |
Why do we use n = 16 * n + hexdigit
to compute the value ? The figure below gives the reason.
Similarly, we can use n = 8 * n + octdigit
to convert octal string, we can use n = 2 * n + bindigit
to convert binary string, too.
A program to convert hexadecimal string.
Reference