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:
- Char * PTR = "123 ";
- Int n = 0;
- N = atoi (PTR );
- Printf ("% d/N", N );
- // Output: 123
However, the atoi library function can only convert a decimal string to an int integer. For 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 impossible to convert "0x11" to a decimal integer 17 using the atoi function. If used, the following results are output:
- Int N;
- Char * STR = "0x11 ";
- N = atoi (STR); // the return value n is equal to 0 (apparently not the expected result)
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.
- # Include <iostream>
- # Include <afx. h>
- Int changenum (cstring STR, int length)
- {
- Char revstr [16] = {0}; // based on the length of the hexadecimal string. Note that the array should not be out of bounds.
- 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
- */
Yes, the above method can get the value we want. Is there a simpler way? 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
- */
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:
- # 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 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:
- # 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
- */
In the MFC program of VC 6.0, we can use the strtol function to convert the handle sometimes, for example:
- 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:
- // 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 ");
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!
The problem of converting a hexadecimal string to a decimal Value