This is a piece of code that I keyed out of the kernel, which is used to pass in a character, which can be output in decimal number format with printf statement%d, and also in the form of an%p address.
The code is as follows:
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #define ToLower (c) __tolower (c) #define ToUpper (c) __ ToUpper (c) static inline unsigned char __tolower (unsigned char c) {//determines if character C is uppercase English letter description: Returns a value other than 0 if the argument c is an uppercase English letter (A-Z), otherwise returns zero. if (Isupper (c)) c-= ' a '-' a '; return c;} Static inline unsigned char __toupper (unsigned char c) {//determines if character C is lowercase English letter description: Returns a value other than 0 if the argument c is a lowercase English letter (A-Z), otherwise returns zero. if (Islower (c)) c-= ' A '-' a '; return c;} int Hex_to_bin (char ch) {if ((ch > ' 0 ') && (ch <= ' 9 ')) return CH-' 0 '; ch = tolower (ch); if ((Ch >= ' a ') &A mp;& (ch <= ' F ')) return CH-' a ' + ten; return-1;} int main (void) {printf ("%d\n", Hex_to_bin (' 1 '));p rintf ("%d\n", Hex_to_bin (' F '));p rintf ("%d\n", Hex_to_bin (' a ')); printf ("%d\n", Hex_to_bin (' 9 '));p rintf ("%p\n", Hex_to_bin (' 1 '));p rintf ("%p\n", Hex_to_bin (' F '));p rintf ("%p\n", Hex _to_bin (' a '));p rintf ("%p\n", Hex_to_bin (' 9 ')); return 0;}
Operation Result:
Linux kernel algorithm---hex_to_bin sharing