C + + hexadecimal string to numeric (numeric)

Source: Internet
Author: User

=================================================
This article is Khler original, reproduced must ensure that this article complete and complete retain the original author information and the original link of this article
e- Mail: [email protected]
qq:23381103
MSN: [email protected]
=================================================

There are two main methods that are actually used for existing functions: method 1:sscanf ()Function Name: sscanf 
Function: Format input from a string 
Usage: int sscanf (char *string, char *format[,argument,...]); The above format%x is the example of formatting a string into 16 digits: #include <stdio.h>
void Main () 

char* p = "0x1a"; 
int nvalude = 0; 
SSCANF (P, "%x", &nvalude); 
printf ("%d\r\n", nvalude); }
Output:
26
method 2:strtol ()Function Name: strtol 
Function: Converts a string to a long integer 
Usage: Long strtol (char *str, char **endptr, int base); The base above is what we're going to convert to a few decimal numbers.
Example: #include <stdio.h> 
#include <stdlib.h> 
void Main () 

char* p = "0x1b"; 
char* str;
Long i = Strtol (p, &str, 16);
printf ("%d\r\n", I); }
Output:
27 In fact, there is another way, is a string array initialized to 0~9~a~f, that is, a hexadecimal corresponding table, the corresponding table can be used to calculate the value of a hexadecimal string, but this hair is too troublesome, not recommended to use.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + hexadecimal string to numeric (numeric)

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.