devxlogo

Converting a hex String to int

Converting a hex String to int

In a previous tip, I showed how to convert a string containing a hexadecimal to int using the sscanf() function. stringstream objects can perform this feat too, in a more object-oriented fashion. In the following program, a string containing a hexadecimal value is converted to int by using an istringstream object. First, the programmer instantiates an istringstream object and initializes it with the hexadecimal string. Then, the stringstream’s setf() member function is called with the ios::hex flag. Finally, the istringstream object writes a decimal value that is equivalent to the hexadecimal value of the original string into the integer n:

 #include #include using namespace std;int main(){ string mem_addr =
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist