Convert Hexadecimal string to decimal number in C++ - c++

I want to convert a hexadecimal string to a decimal number (integer) in C++ and tried with following ways:
std::wstringstream SS;
SS << std::dec << stol(L"0xBAD") << endl;
But it returned 0 instead 2989.
std::wstringstream SS;
SS << std::dec << reinterpret_cast<LONG>(L"0xBAD") << endl;
But it returned -425771592 instead 2989.
But, when I use it like below, it works fine and gives 2989 as expect.
std::wstringstream SS;
SS << std::dec << 0xBAD << endl;
But I want to input a string and get 2989 as output, instead integer input like 0xBAD. For example, I want to input "0xBAD" and cast it to integer and then convert to a decimal number.
Thanks in advance.

// stol example
#include <iostream> // std::cout
#include <string> // std::string, std::stol
int main ()
{
std::string str_dec = "1987520";
std::string str_hex = "2f04e009";
std::string str_bin = "-11101001100100111010";
std::string str_auto = "0x7fffff";
std::string::size_type sz; // alias of size_t
long li_dec = std::stol (str_dec,&sz);
long li_hex = std::stol (str_hex,nullptr,16);
long li_bin = std::stol (str_bin,nullptr,2);
long li_auto = std::stol (str_auto,nullptr,0);
std::cout << str_dec << ": " << li_dec << '\n';
std::cout << str_hex << ": " << li_hex << '\n';
std::cout << str_bin << ": " << li_bin << '\n';
std::cout << str_auto << ": " << li_auto << '\n';
return 0;
}

Related

Convert string into hex format and append "0x " to hex value

I need to convert string to hex format and append "0x" prefix to hex value.
For Example:
Input: std::string s = "0x06A4";
Output: int num = 0x06A4
I have tried this code:
{
std::stringstream ss;
std::string s = "0x06A4";
int num = std::stoi(s, 0, 16);
std::cout << "value in decimal = " << num << '\n';
std::cout << "value in hexadecimal = " << std::hex << num << '\n';
ss << "0x" << std::hex << num << '\n'; //
std::string res = ss.str();
std::cout << "result " << res << '\n';
}
#yogita, std::hex is just one of the configuration you need. You are probably missing the setfill and the setw configuration, as following:
#include <iostream>
#include <sstream>
#include <iomanip>
int main()
{
std::stringstream ss;
std::string s = "0x06A4";
int num = std::stoi(s, nullptr, 16);
std::cout << "value in decimal = " << num << '\n';
std::cout << "value in hexadecimal = " << std::hex << num << '\n';
ss << "0x" << std::hex << std::setfill('0') << std::setw(4) <<num << '\n';
std::string res = ss.str();
std::cout << "result " << res << '\n';
return 0;
}

c++11. Is it possible to do this: string foo = cout << (.00000023)*(.00000023) << endl; -with precision

I want to save the output from
std::cout << endl << std::setprecision(20) << std::fixed;
cout << ((.0000023)*(.00000023))<< endl;
Which is: 0.00000000000052900000
-into a string variable.
You could split this into steps using std::ostringstream:
std::ostringstream output;
output << << endl << std::setprecision(20) << std::fixed;
output << ((.0000023)*(.00000023))<< endl;
std::string result_string = output.str();
I suggest to try std::ostringstream:
std::ostringstream oss;
oss << ...;
std::string result(oss.str());
Note that it's slow on some platfroms/compilers.
See std::stringstream to stream into a string.
#include <string>
#include <sstream>
int main()
{
std::ostringstream stream;
stream << 10 + 10;
std::string result = stream.str(); // result contains "20"
return 0;
}
Edit: String manipulators work with std::ostringstream just like std::cout.
stream << std::fixed << (1.0 / 100.0); // result will contain "0.010000"

Converting hex string to negative double

I'm trying to convert hex strings to double values, where some of the values are negative. This answer works fine for positive double hex strings, but when the double is negative I get a std::out_of_range exception. Why is that? Is there a way to get around it?
I have also tried to do the same thing with stringstream, and that works just fine for both positive and negative hex strings. So there is always that solution, but to me the union approach looks a bit more elegant. A matter of taste I guess.
Sample code:
#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
int main()
{
std::string str1("3fb999999999999a"); // 0.1
std::string str2("bfb999999999999a"); // -0.1
// ******************************
// UNION
// ******************************
union uint64double
{
unsigned long long i;
double d;
};
uint64double val1;
val1.i = std::stoll(str1, nullptr, 16);
uint64double val2;
val2.i = std::stoll(str2, nullptr, 16); // This throws std::out_of_range exception
const int w = 18;
std::cout << "USING UNION" << std::endl;
std::cout << std::setw(w) << "HEX" << std::setw(w) << "DOUBLE" << std::endl;
std::cout << std::setw(w) << str1 << std::setw(w) << val1.d << std::endl;
std::cout << std::setw(w) << str2 << std::setw(w) << val2.d << std::endl;
std::cout << std::endl;
// ******************************
// STRINGSTREAM
// ******************************
unsigned long long i1;
std::stringstream ss;
ss << std::hex << str1;
ss >> i1;
double d1(reinterpret_cast<double&>(i1));
unsigned long long i2;
ss.clear();
ss << std::hex << str2;
ss >> i2;
double d2(reinterpret_cast<double&>(i2));
std::cout << "USING STRINGSTREAM" << std::endl;
std::cout << std::setw(w) << "HEX" << std::setw(w) << "DOUBLE" << std::endl;
std::cout << std::setw(w) << str1 << std::setw(w) << d1 << std::endl;
std::cout << std::setw(w) << str2 << std::setw(w) << d2 << std::endl;
std::cout << std::endl;
return 0;
}
Output:
USING UNION
HEX DOUBLE
3fb999999999999a 0.1
bfb999999999999a 1.#QNAN
USING STRINGSTREAM
HEX DOUBLE
3fb999999999999a 0.1
bfb999999999999a -0.1
I'm using VS2012
Of course stoll throws std::out_of_range exception on input "bfb999999999999a", because 0xbfb999999999999a > LLONG_MAX. If you use stoull instead, everything works nicely, because 0xbfb999999999999a <= ULLONG_MAX.
You can pass the hex string into the stringstream constructor to clean it up a bit. Your std::hex is in the wrong place. std::hex tells the stream to treat the input as a hex value, rather than just a bunch of characters.
I would put this functionality in its own function too, to make it cleaner:
#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
double hexToDouble(const std::string& hex)
{
double d = 0;
std::stringstream ss(hex);
ss >> std::hex >> reinterpret_cast<uint64_t&>(d); // I feel dirty
return d;
}
int main()
{
std::string str1("3fb999999999999a"); // 0.1
std::string str2("bfb999999999999a"); // -0.1
std::cout << str1 << ' ' << hexToDouble(str1) << std::endl;
std::cout << str2 << ' ' << hexToDouble(str2) << std::endl;
return 0;
}

c++ std::istringstream weird std::hex behaviour

EDIT:
I managed to get the same problem on a smaller scale:
std::istringstream hex;
std::string str = "0x7ffa428ab946";
std::cout << "str " << str << std::endl;
hex.str(str);
long caller;
hex >> std::hex >> caller;
std::cout << "caller " << caller << std::endl;
str = "0x7ff9ec0010f0";
std::cout << "str " << str << std::endl;
hex.str(str);
long address;
hex >> std::hex >> address;
std::cout << "address " << address << std::endl;
and get this:
str 0x7ffa428ab946
caller 140712834939206
str 0x7ff9ec0010f0
address 0
why is that?
hex >> std::hex >> caller;
will set eofbit on hex, but the subsequent
hex.str(str);
doesn't clear it. Thus, later attempts to extract from hex will simply fail.
Call hex.clear() after the hex.str(str); call to clear the flags.

Can you create a string in a similar fashion to std::cout?

The following statement pipes all sorts of output to the console as a single string of text
std::cout << "Hi, my name is " << name_as_string << " and I am " << age_as_int << " years old, while weighing " << weight_as_double << " kilograms.";
Can we use this same syntax to build a string in a string variable? How is it done?
#include <sstream>
std::ostringstream ss;
ss << "Hi, my name is " << name_as_string;
ss << " and I am " << age_as_int << " years old, while weighing ";
ss << weight_as_double << " kilograms.";
std::string str = ss.str();
You can also use std::istringstream for multiple input, and std::stringstream for both input and output.
std::string str = "1 2 3 4 5";
std::istringstream ss(str);
int i;
while( ss >> i) {
std::cout << i;
}
stringstream will rescue you here;
#include <sstream>
std::stringstream ss;
ss << stuff << to << output;
std::string s = ss.str();
Use std::ostringstream
By using std::stringstream :
#include <sstream>
#include <iostream>
int main()
{
std::stringstream ss;
ss << "Hi, my name is " << name_as_string << " and I am " << age_as_int << " years old, while weighing " << weight_as_double << " kilograms.";
std::cout<<ss.str()<<std::endl;
}