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.
Related
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;
}
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;
}
I need to print hex and decimal values in my console. I used the following piece of code to do it.
std::stringstream ss;
ss << "0x" << std::uppercase << std::hex << 15;
std::cout << ss.str() << std::endl;
ss.str("");
ss.clear();
ss << 15;
std::cout << ss.str() << std::endl;
But I am getting both values in Hex format. How to reset stringstream?
How to reset stringstream?
Format flags are sticky.
You can save the old format flags to restore them later:
std::stringstream ss;
auto oldFlags = ss.flags(); // <<
ss << "0x" << std::uppercase << std::hex << 15;
std::cout << ss.str() << std::endl;
ss.flags(oldFlags); // <<
ss << 15;
std::cout << ss.str() << std::endl;
Assuming you know which formatting flag are actually used, you can just save their original value and restore them later. When the set of formatting flags being changed is large and possibly somewhat out of the control of the function, there are essentially two approaches which are both, unfortunately, not really very cheap:
Do not use the std::stringstream directly but rather use a temporary stream to which the format flags get applied:
{
std::ostream out(ss.rdbuf());
out << std::showbase << std::uppercase << std::hex << 15;
}
ss << 15;
You can use the copyfmt() member to copy all the formatting flags and later restore these:
std::ostream aux(0); // sadly, something stream-like is needed...
out.copyfmt(ss);
out << std::showbase << std::uppercase << std::hex << 15;
ss.copyfmt(aux);
out << 15;
Both approaches need to create a stream which is, unfortunately, not really fast due to a few essentially mandatory synchronizations (primarily for creating the std::locale member).
Just use
std::nouppercase
ss << std::uppercase;
...
ss << std::nouppercase;
...
I am still working on a database for movies and I would like to show the user what he has input to the file.
However when i use cout << lisafilm << it provides me with hex value. Therefore, I need to conver hex to string.
Snippet of trouble.
void sisend()
{
string nimi;
int aasta;
long int hinne;
string vaadatud;
ofstream lisafilm("andmebaas.txt", ios::app);
cout <<"Sisestage filmi nimi." << endl;
cin >> nimi;
cout << "Sisestage filmi aasta." << endl;
cin >> aasta;
cout << "Sisestage filmi hinne." << endl;
cin >> hinne;
cout << "Kas olete filmi juba vaadanud?" << endl;
cout << "Vastake 'Jah' voi 'Ei'" << endl;
cin >> vaadatud;
lisafilm<< nimi << " " << aasta << " " << hinne<< " " << vaadatud << endl;
lisafilm.close();
{
system("CLS");
int hex_str = lisafilm ;
cout << "Aitah kasutamast andmebaasi." << endl;
system("pause");
cin.get ();
}
main();
}
when i use cout << lisafilm it provides me with hex value
This is because you are trying to output an ofstream. When this happens, operator void* gets called, producing an arbitrary hex sequence which is tied to your stream, but ultimately is very much useless.
Try this:
std::stringstream ss;
ss << std::hex << lisafilm;
const std::string s = ss.str();
lisafilm is a stream, not a string
If you want to copy lisafilm to cout something like cout << lisafilm.rdbuf(); would do the trick (assuming that lisafilm is an ostream or istream and that lisafilm's position is the start of the file.
Your code is very poorly formatted, I don't think what you posted would compile. If you clean it up stackoverflow may be able to help you more.
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;
}