Software Development

Totally different Methods to Convert Hex String to Integer in C++ STL


A hexadecimal quantity is a quantity 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 particular properties that assist to transform a hexadecimal string or quantity to a decimal quantity simply. 

There are 5 alternative ways to transform a Hex string to an Integer in C++:

  1. Utilizing stoi() operate
  2. Utilizing sscanf() operate
  3. Utilizing stoul() operate
  4. Utilizing string stream methodology
  5. Utilizing increase:lexical_cast operate

Let’s begin discussing every of those strategies intimately.

1. Utilizing C++ STL stoi() operate

stoi() is an STL operate in c++ that’s used to transform any quantity(binary, octal, hexadecimal) to an unsigned integer in a specified base. It’s outlined within the <string> header file. It comprises three parameters:

  • First is the string title, which suggests the string that you simply wish to convert into decimal. 
  • Second is an index quantity, by default, it’s 0, or you may initialize it with nullptr. 
  • The final one is the bottom of the enter string. It is usually an non-obligatory parameter. By default, it’s 10. 

This operate returns an integer worth of the given hex string.

Syntax:

int stoi (const string&  str, [size_t* idx], [int base]);

Beneath is the C++ program to implement stoi() operate to transform a hex string to an integer:

C++

#embrace <iostream>

#embrace <string.h>

  

utilizing namespace std;

  

int principal()

  

  string s = "DD";

  

  

  

  

  int ans = stoi(s, 0, 16);

  

  cout << ans << endl;

  return 0;

2. Utilizing C++ STL sscanf() operate

sscanf operate is mainly used to learn the info from a string buffer.

Syntax:

int sscanf( const char* buffer, const char* format,….);

Right here the buffer is a pointer to a null-terminated character string to learn the info and the format pointer to a null-terminated character string specifies find out how to learn the info, and lastly, we are going to add yet one more parameter to the place to retailer the worth.

Beneath is the C++ program to transform a hex string to an integer utilizing sscanf() operate:

C++

#embrace <bits/stdc++.h>

utilizing namespace std;

  

int principal()

  string s = "DD";

  int ans;

    

  

  

  

  sscanf(s.c_str(), "%x", &ans);

  cout << ans << endl;

  return 0;

3. Utilizing C++ STL stoul() operate

That is an STL operate in C++11 that’s outlined within the header file <string>, which can be used to transform a string to an integer.

Syntax: 

stoul (const string&  str, size_t* idx, int base);

Parameters:

  • str: Enter string, with the illustration of an integral quantity.
  • idx: Pointer to an object of kind size_t, whose worth is about by 0. It can be null pointer, by which case it’s not used.
  • base: Numerical base(radix) that determines the legitimate characters and their interpretation. Right here it will likely be 16, because it’s a hexadecimal string.

Beneath is the C++ program to transform a hex string to an Integer utilizing stoul() operate:

C++

#embrace<bits/stdc++.h>

utilizing namespace std;

  

int principal()

  string s = "DD";

        

  

  

  

  

  

  int i = stoul(s, 0, 16);

    

  cout << i << endl;    

  return 0;

4. Utilizing C++ STL string stream methodology

When the bottom area format is about to hex, the integer values of that hex worth are saved within the stream. It’s completed by utilizing a hex manipulator. 

Beneath is the C++ program to implement stringstream methodology to transform a hex string to an integer:

C++

#embrace <bits/stdc++.h>

utilizing namespace std;

  

int principal()

  string s = "DD";

  int i;

  

  

  istringstream iss(s);

  

  

  

  iss >> hex >> i;

  

  cout << i << endl;

  

  return 0;

5. Utilizing C++ STL increase:lexical_cast operate

In c++ STL there’s a operate referred to as a lift, which can be utilized to transform a hex string to an integer. It first streams the string after which it converts it to an integer with increase::lexical_cast<int>.

Beneath is the C++ program to implement increase:lexical_cast operate to transform a hex string to an integer:

C++

#embrace <iostream>

#embrace <string>

#embrace <increase/lexical_cast.hpp>

utilizing namespace std;

   

int principal()

  string s = "DD";

        

  

  int i = increase::lexical_cast<int>(s);

  cout << i << endl; 

  return 0;

Output:

221

What's your reaction?

Leave A Reply

Your email address will not be published. Required fields are marked *