C language: Convert 16 binary string to int type value

Source: Internet
Author: User
Tags uppercase letter

Converts a 16-binary string value to an int integer value

This example uses "1de" as the test string, the implementation code is as follows:

[CPP]View Plaincopy
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. /*
  6. * Convert characters to numeric values
  7. * */
  8. int c2i (char ch)
  9. {
  10. //If it is a number, subtract 48 from the ASCII code of the number if ch = ' 2 ', then ' 2 '-= 2
  11. if (isdigit (CH))
  12. return ch-48;
  13. //If it is a letter, but not a~f,a~f returns
  14. if (Ch < ' A ' | | (Ch > ' F ' && ch < ' a ') | | ch > ' z ')
  15. return-1;
  16. //If it is an uppercase letter, subtract 55 from the ASCII code of the number if ch = ' A ', then ' a '-= ten
  17. //If it is a lowercase letter, subtract 87 from the ASCII code of the number, if ch = ' A ', then ' a '-= ten
  18. if (isalpha (CH))
  19. return Isupper (CH)? ch-55:ch-87;
  20. return-1;
  21. }
  22. /*
  23. * Function: Convert hexadecimal string to integer (int) value
  24. * */
  25. int Hex2dec (char *hex)
  26. {
  27. int len;
  28. int num = 0;
  29. int temp;
  30. int bits;
  31. int i;
  32. //In this example hex = "1de" Length is 3, hex is the main function passed
  33. Len = strlen (hex);
  34. For (i=0, temp=0; i<len; i++, Temp=0)
  35. {
  36. ///First time: i=0, * (hex + i) = * (hex + 0) = ' 1 ', i.e. temp = 1
  37. ///second time: I=1, * (hex + i) = * (hex + 1) = ' d ', i.e. temp =
  38. //Third: i=2, * (hex + i) = * (hex + 2) = ' d ', i.e. temp =
  39. temp = c2i (* (hex + i));
  40. //Total 3 bits, one 16 binary bit saved with 4 bit
  41. ///First: ' 1 ' is the highest bit, so temp shifts left (len-i-1) * 4 = 2 * 4 = 8-bit
  42. ///second: ' d ' is a secondary high, so temp shifts left (len-i-1) * 4 = 1 * 4 = 4-bit
  43. //Third: ' E ' is the lowest bit, so temp shifts left (len-i-1) * 4 = 0 * 4 = 0-bit
  44. Bits = (Len-i-1) * 4;
  45. temp = temp << bits;
  46. //You can also use num + = temp here;
  47. num = num | Temp
  48. }
  49. //Return results
  50. return num;
  51. }
  52. int main (int argc, char *argv[])
  53. {
  54. char ch[10] = {0};
  55. strcpy (CH, "1de");
  56. printf ("hex:%d\n", Hex2dec (CH));
  57. return 0;
  58. }




I tested at CentOS 6.5

Compilation: Gcc-wall Test.c-ohex

Run:./hex

Output: hex:478

C language: Convert 16 binary string to int type value

Related Article

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.