Linux kernel algorithm---hex_to_bin sharing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.