0

I got string "FFFF6472", and want to convert it to -39822 like below:

std::string hex = "FFFF6472";
int dec = hex_to_int(hex);
printf("%d", dec); // -39822

Are there any supported functions like 'hex_to_int' in standard library?

WOOSEOK CHOI
  • 165
  • 9
  • 5
    `stoi`/`stol` accepts a `base` parameter, for which you can pass 16: https://en.cppreference.com/w/cpp/string/basic_string/stol. – wohlstad Oct 24 '22 at 07:32
  • 1
    See also [C++ Hex to Int using Stringstream](https://stackoverflow.com/questions/26310530/c-hex-to-int-using-stringstream) – wovano Oct 24 '22 at 07:34
  • Thx guys. These answers also helps. https://stackoverflow.com/questions/1070497/c-convert-hex-string-to-signed-integer – WOOSEOK CHOI Oct 24 '22 at 07:41
  • Also, you can take a look at `std::from_chars` [here](https://en.cppreference.com/w/cpp/utility/from_chars). – Lluís Alemany-Puig Oct 24 '22 at 07:52

0 Answers0