Due to the ASCII character encoding in C. When the remainder ( temp ) is less than ten, the hexadecimal digit is also in the range from 0 to 9. The characters "0" - "9" are in the ASCII range from 48 to 57.
When the remainder is greater than 10 (and always less than 15, due to the operation of the remainder % ), the hexadecimal digit is in the range from A to F, which in ASCII is in the range from 65 to 70. Thus, temp + 55 is a number from 65 to 70 and thus gives a character in the range of 'A' to 'F'.
The most commonly used string is char[] digits = "0123456789ABCDEF"; and use the remainder as an index on this line. The method in your question (maybe) also works.
source share