Article Source: http://dev.firnow.com/course/3_program/c++/cppjs/20091010/178409.html
In a program, you sometimes need to convert a hexadecimal string to a decimal number. Like what:
Char *ptr = "0x11";
int n = 0;
We want to make n equal to 0x11, that's 17.
Normally, in C, we want to convert a string to an integer number, usually using the following method:
Char *ptr= "123"; int n=0; N=atoi (PTR); printf ("%d/n", N); Output: 123
However, the Atoi library function can only convert decimal strings to int integers, such as the following example:
#include <stdlib.h> #include <stdio.h>//atoi header file int main (void) {int n; char *str = "12345.67"; n = atoi (str); int atoi (const char *nptr); printf ("string =%s integer =%d/n", str, n); return 0; }/* Output: string = 12345.67 integer = 12345 * *
Therefore, it is not possible to convert "0x11" to decimal integer 17 with the Atoi function. If used, the following results are output:
int n; Char *str = "0x11"; n = atoi (str); return value n equals 0 (obviously not the result we want)
How about that. Then some people would like to do it, we write a function of the transformation of the line, such as the following method:
Note: We use VC6.0 to establish a WIN32 console program, in order to facilitate the application of the CString type variable, need to make a little change.
(1) contains afx.h header file
(2) in Project->settings->general->mircrosoft Foundation classes, select Use MFC in a Shared DLL Then we can use the CString variable under the WIN32 console, otherwise there will be a compilation error.
#include <iostream> #include <afx.h> int changenum (CString str,int length) {char revstr[16]={0};//////per hexadecimal string The length of this note here is that the array is not crossed int num[16]={0}; int count=1; int result=0; strcpy (REVSTR,STR); for (int i=length-1;i>=0;i--) {if (revstr[i]>= ' 0 ') && (revstr[i]<= ' 9 ')) num[i]=revstr[i]-48;// The ASCII value of character 0 is ' if ' ((revstr[i]>= ' a ') && (revstr[i]<= ' F ')) num[i]=revstr[i]-' a ' +10; else if ((revstr[i]>= ' a ') && (revstr[i]<= ' F ')) num[i]=revstr[i]-' a ' +10; else num[i]=0; Result=result+num[i]*count; count=count*16;//16 (if octal is here multiplied by 8)} return result; int main () {CString str= "0x11"; int n=0; N=changenum (str,str. GetLength ()); printf ("%d/n", N); return 0; }/* Output: 17/*
Yes, the above method can get the value we want. Is there a simpler way to do it? Of course.
Method 1:
#include <stdio.h> int main () {char szvalue[] = "0x11"; int nvalude = 0; sscanf (szvalue, "%x", &nvalude); printf ( "%d/n", nvalude); return 0; }/* Output: 17/*
This library function is mainly used to sscanf :
Function Name: sscanf
Function: Performs formatted input from a string
Usage: int sscanf (char *string, char *format[, argument, ...)); %x is the type we want to format, that is, output hexadecimal
Method 2:
#include <stdio.h> #include <stdlib.h>//strtol header file int main () {char *p= "0x11"; char *str; int i = (int) strtol ( P, &str, 16);/hexadecimal printf ("%d/n", I); return 0; }/* Output: 17/*
The main use of this library function is strtol , which uses the following methods:
Function Name: strtol
Function: Converts a string to a long integer
Usage: Long strtol (char *str, char **endptr, int base); base indicates that we want to convert to several numbers
program Example:
#include <stdlib.h> #include <stdio.h> int main (void) {char *string = "0x11", *endptr long lnumber;/* Strto L converts string to long integer */lnumber = Strtol (String, &endptr, 16); printf ("string =%s long =%ld/n", string, Lnumber); return 0; }/* Output: string = 0x11 Long = 17 * *