In C language, convert the string (decimal and hexadecimal) to a decimal number.

Source: Internet
Author: User

In C language, convert the string (decimal and hexadecimal) to a decimal number.

The following code extracts the hexadecimal and hexadecimal numbers of a string:

# Include
 
  
Typedef char TUINT8; typedef int TUINT32; TUINT32 Read_DecNumber (const TUINT8 * str); TUINT32 Read_HexNumber (const TUINT8 * str); int main (void) {int ret = Read_DecNumber ("1000"); int d = Read_HexNumber ("A"); printf ("converts the number in the string to A decimal number: % d \ n ", ret); printf (" convert the hexadecimal number in the string to the hexadecimal number: % d \ n ", d); return 0 ;} // convert the number in the string to the 10th digit TUINT32 Read_DecNumber (const TUINT8 * str) {TUINT32 value; if (! Str) {return 0;} value = 0; while (* str> = '0') & (* str <= '9 ')) {value = value * 10 + (* str-'0'); str ++;} return value ;} // convert the hexadecimal number in the string to the hexadecimal number TUINT32 Read_HexNumber (const TUINT8 * str) {TUINT32 value; if (! Str) {return 0;} value = 0; while (1) {if (* str> = '0') & (* str <= '9 ')) {value = value * 16 + (* str-'0');} else if (* str> = 'A ') & (* str <= 'F') {value = value * 16 + (* str-'A') + 10 ;} else if (* str> = 'A') & (* str <= 'F') {value = value * 16 + (* str-'A ') + 10;} else {break;} str ++;} return value ;}
 
Running result:


 

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.