String to int

Hello! I have string with hex number haw can i make it int number ?

1
2
3
4
5

  string a = "7D"; //125
  ...
  int b = 125;
when you use c++11 you can use std::stoi

1
2
3
4
5
6
7
8
9
10
11
// Example program
#include <iostream>
#include <string>

int main()
{
    std::string a = "7D";
    int b = std::stoi(a, nullptr, 16);
    
    std::cout << b << std::endl;
}


when using the old standard stringstream is the way to go:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Example program
#include <iostream>
#include <string>
#include <sstream>

int main()
{
    std::string a = "7D";
    std::stringstream ss(a);
    int b;
    
    ss >> std::hex >> b;
    
    std::cout << b << std::endl;
}
Thank you!
Topic archived. No new replies allowed.