How to use C++ STL boost:lexical_cast function In C++
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:
C++
// C++ program to implement // boost:lexical_cast function // to convert a hex string to // an integer: #include <iostream> #include <string> #include <boost/lexical_cast.hpp> using namespace std; // Driver code int main() { string s = "DD" ; // Converting the string to integer int i = boost::lexical_cast< int >(s); cout << i << endl; return 0; } |
Output:
221
Different Ways to Convert Hex String to Integer in C++ STL
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++:
- Using stoi() function
- Using sscanf() function
- Using stoul() function
- Using string stream method
- Using boost:lexical_cast function
Let’s start discussing each of these methods in detail.