In programs, we sometimes need to convert a hexadecimal string to a decimal number. For example:
Char * PTR = "0x11 ";
Int n = 0;
// We want n to be equal to 0x11, that is, 17
In C, we usually use the following method to convert a string to an integer:
[CPP]
View plaincopyprint?
- Char * PTR = "123 ";
- Int n = 0;
- N = atoi (PTR );
- Printf ("% d/N", N );
- // Output: 123
Char * PTR = "123"; <br/> int n = 0; <br/> N = atoi (PTR ); <br/> printf ("% d/N", n); <br/> // output: 123
However, the atoi library function can only convert a decimal string to an int integer. For example:
[CPP]
View plaincopyprint?
- # 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
- */
# Include <stdlib. h> <br/> # include <stdio. h> // atoi header file <br/> int main (void) <br/>{< br/> int N; <br/> char * STR = "12345.67 "; <br/> N = atoi (STR); // int atoi (const char * nptr ); <br/> printf ("string = % s integer = % d/N", STR, n); <br/> return 0; <br/>}< br/>/* output: <br/> string = 12345.67 integer = 12345 <br/> */
Therefore, it is impossible to convert "0x11" to a decimal integer 17 using the atoi function. If used, the following results are output:
[CPP]
View plaincopyprint?
- Int N;
- Char * STR = "0x11 ";
- N = atoi (STR); // the return value n is equal to 0 (apparently not the expected result)
Int N; <br/> char * STR = "0x11"; <br/> N = atoi (STR ); // return value n is equal to 0 (obviously not the expected result) <br/>
What should we do? At this time, someone will think, it's easy to do. We can write a function to convert it, for example, using the following method:
Note: We have created a Win32 console program with VC 6.0. For convenience, the program uses the cstring type variable and requires some modifications.
(1) include the afx. h header file
(2) in project-> Settings-> General-> Mircosoft foundation classes, select use MFC in a shared DLL
Then we can use the cstring variable in the Win32 console. Otherwise, a compilation error occurs.
[CPP]
View plaincopyprint?
- # Include <iostream>
- # Include <afx. h>
- Int changenum (cstring STR, int length)
- {
- Char revstr [16] = {0 };
// According to the length of the hexadecimal string, note that the array should not cross the border.
- 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 48
- Else 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; // hexadecimal (if it is octal, multiply it here 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
- */
# Include <iostream> <br/> # include <afx. h> <br/> int changenum (cstring STR, int length) <br/>{< br/> char revstr [16] = {0 }; // according to the length of the hexadecimal string, note that the array should not be out of bounds <br/> int num [16] = {0}; <br/> int COUNT = 1; <br/> int result = 0; <br/> strcpy (revstr, STR); <br/> for (INT I = length-1; I >= 0; I --) <br/>{< br/> If (revstr [I]> = '0') & (revstr [I] <= '9 ')) <br/> num [I] = revstr [I]-48; // The ASCII value of 0 is 48 <br/> else if (revstr [I]> = 'A ') & (revstr [I] <= 'F') <br/> num [I] = revstr [I]-'A' + 10; <br/> else if (revstr [I]> = 'A') & (revstr [I] <= 'F ')) <br/> num [I] = revstr [I]-'A' + 10; <br/> else <br/> num [I] = 0; <br/> result = Result + num [I] * count; <br/> COUNT = count * 16; // hexadecimal (if it is octal, multiply it here by 8) <br/>}< br/> return result; <br/>}< br/> int main () <br/>{< br/> cstring STR = "0x11"; <br/> int n = 0; <br/> N = changenum (STR, str. getlength (); <br/> printf ("% d/N", n); <br/> return 0; <br/>}< br/>/* output: <br/> 17 <br/> */
Yes, the above method can get the value we want. Is there a simpler way? Of course!
Method 1:
[CPP]
View plaincopyprint?
- # Include <stdio. h>
- Int main ()
- {
- Char szvalue [] = "0x11 ";
- Int nvalude = 0;
- Sscanf (szvalue, "% x", & nvalude );
- Printf ("% d/N", nvalude );
- Return 0;
- }
- /* Output:
- 17
- */
# Include <stdio. h> <br/> int main () <br/>{< br/> char szvalue [] = "0x11"; <br/> int nvalude = 0; <br/> sscanf (szvalue, "% x", & nvalude); <br/> printf ("% d/N", nvalude); <br/> return 0; <br/>}< br/>/* output: <br/> 17 <br/> */
The library function sscanf is mainly used:
Function Name: sscanf
Function: format the input from the string.
Usage: int sscanf (char * string, char * Format [, argument,...]); // % x is the type we want to format, that is, the output hexadecimal
Method 2:
[CPP]
View plaincopyprint?
- # 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
- */
# Include <stdio. h> <br/> # include <stdlib. h> // strtol header file <br/> int main () <br/> {<br/> char * P = "0x11 "; <br/> char * STR; <br/> int I = (INT) strtol (p, & STR, 16 ); // hexadecimal <br/> printf ("% d/N", I); <br/> return 0; <br/>}< br/>/* output: <br/> 17 <br/> */
The strtol library function is mainly used in the following ways:
Function Name: strtol
Function: converts a string to a long integer.
Usage: Long strtol (char * STR, char ** endptr, int base); // base indicates the number of hexadecimal numbers to be converted.
Program example:
[CPP]
View plaincopyprint?
- # Include <stdlib. h>
- # Include <stdio. h>
- Int main (void)
- {
- Char * string =
"0x11", * endptr;
- Long lnumber;
- /* Strtol 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
- */
# Include <stdlib. h> <br/> # include <stdio. h> <br/> int main (void) <br/>{< br/> char * string = "0x11", * endptr; <br/> long lnumber; <br/>/* strtol converts string to long integer */<br/> lnumber = strtol (string, & endptr, 16 ); <br/> printf ("string = % s long = % LD/N", String, lnumber); <br/> return 0; <br/>}< br/>/* output: <br/> string = 0x11 long = 17 <br/> */
In the MFC program of VC 6.0, we can use the strtol function to convert the handle sometimes, for example:
[CPP]
View plaincopyprint?
- Handle handle = (handle) strtol (STR, null, 16 );
Handle handle = (handle) strtol (STR, null, 16 );
PS: What is a handle?
A handle is a 32-bit integer that uniquely identifies a variety of memory objects.
Some are unique in the system (such as window handle), and some are unique in the current process or thread (such as thread handle, with another identifier globally ).
The details can be divided into many types, all starting with H. Use long in VB.
Common include Windows handle (hwnd), device description table handle (HDC), memory handle (hmem), file handle, Process Handle, thread handle, and pen type handle (Hpen ), font handle (hfont), area handle (hrgn), and so on.
When applying for a handle, resources are occupied, which can be divided into three types: system, user, and GDI. Windows resources are fixed and do not expand as the memory expands. Therefore, you must release the resources after use.
Method 3:
On the Internet, I also saw a friend put forward a method to read and write INI files (I think this method is too inefficient, after all, to read and write files) and extract it as follows:
[CPP]
View plaincopyprint?
- // Store handle
- Struct tag_struct
- {
- Hwnd;
- };
- Struct tag_struct struct;
- Struct. hwnd = m_hwnd;
- // Write the struct containing the handle to the INI File
- Writeprivateprofilestruct ("section", "key", & struct, sizeof (struct), "C: // 1.ini ");
- // Read handle
- Struct tag_struct
- {
- Hwnd;
- };
- Struct tag_struct struct;
- // Read handle from the INI file containing the handle struct
- Getprivateprofilestruct ("section", "key", & struct, sizeof (struct), "C: // 1.ini ");
// Store handle <br/> struct tag_struct <br/>{< br/> hwnd; <br/>}; <br/> struct tag_struct struct; <br/> struct. hwnd = m_hwnd; <br/> // write the struct containing the handle to the INI file <br/> writeprivateprofilestruct ("section", "key", & struct, sizeof (struct ), "C: // 1.ini"); <br/> // read handle <br/> struct tag_struct <br/>{< br/> hwnd; <br/> }; <br/> struct tag_struct struct; <br/> // read handle from the INI file containing the handle struct <br/> getprivateprofilestruct ("section", "key", & struct, sizeof (struct), "C: // 1.ini ");
The above is a method that I know to convert a hexadecimal string into an integer value. There may be more appropriate methods. Please help me add a comment when I see this article, thank you!