0

How to convert a decimal value to Hexadecimal in visual studio C++.

For example, i want to convert decimal value 125 to Hexadecimal 7D.

I can do this in C# using string hexValue = decValue.ToString("X");

How will i do the same operation in Visual C++.

0

2 Answers 2

1

Use the std::stringstream class to format the number like this:

std::stringstream sstr;
sstr << std::hex << 32768;
std::string hexValue = sstr.str();
2
  • It is giving error: stringstream not a member of std
    – Jake
    May 8, 2014 at 13:52
  • 1
    stringstream is declared in #include <sstream>
    – farindk
    May 8, 2014 at 14:12
-1

Does this help?

https://stackoverflow.com/a/20034349/2064516

std::stringstream ss;
ss  << hex_value ; // std::string hex_value
ss >> std::hex >> decimal_value ; //int decimal_value

std::cout << decimal_value ;
1
  • This is the opposite of the conversion requested.
    – Ben Voigt
    May 8, 2014 at 13:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.