C + + implements a hexadecimal string conversion to a decimal integer method _c language

Source: Internet
Author: User

This article describes how C + + implements a hexadecimal string conversion to a decimal integer. Share to everyone for your reference. The implementation method is as follows:

 * * Converts a string of hexadecimal digits (including an optional prefix of 0x or 0X) to an integral value equivalent to it/* #include <stdio.h> #include <math.h
 >/* Converts characters in hexadecimal to the corresponding integer */int hexchtoi (char hexch) {char phexch[] = "ABCDEF";
 Char qhexch[] = "abcdef";
 int i; for (i=0;i<6;i++) {if (Hexch = = Phexch[i]) | |
   (Hexch = = Qhexch[i])
 Break
 printf ("i=%d", I);
if (I >= 6) {return 0;/* non-hexadecimal character/} return 10+i;
 int Htoi (char s[]) {int n=0;//* has n-bit/int valu=1;/* valid/int i=0,j;
 int answer=0; /* Validity Check/if ((s[0] = = ' 0 ') && ((s[1] = = ' x ') | | (s[1] = = ' X '))
 {i = 2;
    while ((s[i)!= ' \ n ')) {if (S[i] < ' 0 ') && (S[i] > ' 9 ')) {if (Hexchtoi (s[i)) = = 0) {valu=0;
   Break
  }} n++;
 i++;
  } if (Valu!= 0) {for (j=0;j<n;j++) {answer = = (int) POW (16,j) * Hexchtoi (s[i-j-1)));
 } else Answer =-1;
return answer;
 Main () {char *n[] = {"0x7ff0", "0x2341"};
 printf ("%s is%d\n", N[0],htoi (N[0]));
printf ("%s is%d\n", n[0],123); }

I hope this article will help you with your C + + programming.

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.