Different Ways to Convert Hex String to Integer in C++ STL
Last Updated :
08 Nov, 2022
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.
1. Using C++ STL stoi() function
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:
- First is the string name, which means the string that you want to convert into decimal.
- Second is an index number, by default, it's 0, or you can initialize it with nullptr.
- The last one is the base of the input string. It is also an optional parameter. By default, it's 10.
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:
C++
// C++ program to implement stoi()
// function to convert a hex string
// to an integer
#include <iostream>
#include <string.h>
using namespace std;
// Driver code
int main()
{
// Input string
string s = "DD";
// Initializing the return integer
// value of the function to a new
// variable
int ans = stoi(s, 0, 16);
cout << ans << endl;
return 0;
}
2. Using C++ STL sscanf() function
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:
C++
// C++ program to convert a hex
// string to an integer using
// sscanf() function
#include <bits/stdc++.h>
using namespace std;
// Driver code
int main()
{
string s = "DD";
int ans;
// Converting a hexadecimal C-string
// to an integer using the format %x
// and storing it into ans variable
sscanf(s.c_str(), "%x", &ans);
cout << ans << endl;
return 0;
}
3. Using C++ STL stoul() function
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:
- str: Input string, with the representation of an integral number.
- idx: Pointer to an object of type size_t, whose value is set by 0. It can also be null pointer, in which case it is not used.
- base: Numerical base(radix) that determines the valid characters and their interpretation. Here it will be 16, as it's a hexadecimal string.
Below is the C++ program to convert a hex string to an Integer using stoul() function:
C++
// C++ program to convert a hex string
// to an Integer using stoul() function
#include<bits/stdc++.h>
using namespace std;
// Driver code
int main()
{
string s = "DD";
// Converting a hexadecimal string
// to an integer
// Initializing the return integer
// value of the function to a
// new variable
int i = stoul(s, 0, 16);
cout << i << endl;
return 0;
}
4. Using C++ STL string stream method
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:
C++
// C++ program to implement
// stringstream method to
// convert a hex string to
// an integer
#include <bits/stdc++.h>
using namespace std;
// Driver code
int main()
{
string s = "DD";
int i;
// Used for breaking words
istringstream iss(s);
// Converting to integer and
// storing it to i
iss >> hex >> i;
cout << i << endl;
return 0;
}
5. Using C++ STL boost:lexical_cast function
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
Similar Reads
Convert String to Integer Vector in C++
Strings are sometimes used to represent the numerical data which needs to be converted back to integer, but sometimes we may need to convert it to vector of integers instead due to some problems such as too large integer. In this article, we will learn different way to convert a string to integer ve
2 min read
Convert Octal String to Integer using stoi() Function in C++ STL
The octal numeral system, or oct for short, is the base-8 number system and uses the digits 0 to 7, that is to say, 10 octal represents eight, and 100 octal represents sixty-four.How to Convert Octal String To Integer in C++?We can use an in-built library function stoi() which stands for String to I
2 min read
How to Convert String to Date in C++?
In C++, we generally store the date and time data in the form of the object of type std::tm. Sometimes, this data is stored inside a string object and we need to convert it into the tm type for further processing. In this article, we will learn how to convert a string to date in C++. Example: Input:
2 min read
How to Convert a Single Character to String in C++?
A string is a generally made up of a sequence of multiple characters. In this article, we will learn how to convert a single character to a string in C++.Example:Input: c = 'A'Output: s = "a"Explanation: Character c is converted to a string.Input: c = '#'Output: s = "#"Explanation: Character c is co
4 min read
How to Convert Float to int in C++?
In C++, the float is a short term for floating point value that is used to define numeric values with decimal points. The floating point numbers can be converted to integers using the process called type casting. In this article, we will learn how to convert a float value to an int in C++. Example:
2 min read
Convert string to integer without using any in-built functions
Given a string str, the task is to convert the given string into the number without using any inbuilt function. Examples: Input: str = "985632" Output: 985632 Explanation: Given input is in string form and returned output is in integer form. Input: str = "123" Output: 123 Explanation: Given input is
10 min read
How to Convert Hex String to Byte Array in C++?
A Hex String is a combination of the digits 0-9 and characters A-F and a byte array is an array used to store byte data types only. The default value of each element of the byte array is 0. In this article, we will learn how to convert a hex string to a byte array. Example: Input: Hex String : "2f4a
2 min read
How to Convert wstring to int in C++?
In C++, std::wstring is a type of string where each character is of a wide character type. These types of strings can be used to store the numerical strings which can then be converted to their corresponding type. In this article, we will see how to convert a wstring to an int in C++. Input: wstr =
2 min read
How to Convert wstring to double in C++
In C++, std::wstring is a type of string where each character is of a wide character type. Converting wstring to a double in C++ can be a common requirement especially when dealing with the inputs that are stored as unicode characters. In this article, we will learn how to convert a wstring to a dou
2 min read
Convert Float to String In C++
In this article, we learn how we can convert float to string in C++ using different methods: Using the to_string()Using stringstreamUsing MacrosUsing lexical_cast from the boost library1. Using to_string() The to_string() method takes a single integer variable or other data type and converts it into
3 min read