![]() |
VOOZH | about |
A hexadecimal number is a number whose base is 16. has numerals 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, and 15. And 10, 11, 12, 13, 14, and 15 these numbers are denoted by A, B, C, D, E, F. In C++ STL there are certain properties that help to convert a hexadecimal string or number to a decimal number easily.
There are 5 different ways to convert a Hex string to an Integer in C++:
Let's start discussing each of these methods in detail.
stoi() is an STL function in c++ that is used to convert any number(binary, octal, hexadecimal) to an unsigned integer in a specified base. It is defined in the <string> header file. It contains three parameters:
This function returns an integer value of the given hex string.
Syntax:
int stoi (const string& str, [size_t* idx], [int base]);
Below is the C++ program to implement stoi() function to convert a hex string to an integer:
221
sscanf function is basically used to read the data from a string buffer.
Syntax:
int sscanf( const char* buffer, const char* format,....);
Here the buffer is a pointer to a null-terminated character string to read the data and the format pointer to a null-terminated character string specifies how to read the data, and lastly, we will add one more parameter to where to store the value.
Below is the C++ program to convert a hex string to an integer using sscanf() function:
221
This is an STL function in C++11 that is defined in the header file <string>, which is also used to convert a string to an integer.
Syntax:
stoul (const string& str, size_t* idx, int base);
Parameters:
Below is the C++ program to convert a hex string to an Integer using stoul() function:
221
When the base field format is set to hex, the integer values of that hex value are stored in the stream. It is done by using a hex manipulator.
Below is the C++ program to implement stringstream method to convert a hex string to an integer:
221
In c++ STL there is a function called a boost, which can be used to convert a hex string to an integer. It first streams the string and then it converts it to an integer with boost::lexical_cast<int>.
Below is the C++ program to implement boost:lexical_cast function to convert a hex string to an integer:
Output:
221