I want to know how can I make the string I converted from DWORD to onstringstream and then to AnsiString.
But that doesn't really matter, the conversion could be from int to string, I just want to know how I can make every string converted to ALWAYS show 6 digits, like if my number is 57, in the string it will be 000057.
Thanks!
Use io manipulators setfill and setw:
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
int main()
{
std::ostringstream s;
s << std::setfill('0') << std::setw(6) << 154;
std::cout << s.str() << "\n";
return 0;
}
So, the question about formatted output?
you can use iostream::width and `iostream::fill':
// field width
#include <iostream>
using namespace std;
int main () {
cout << 100 << endl;
cout.width(6);
cout.fill('0');
cout << 100 << endl;
return 0;
}
Related
I want to store the hex values into a string, but I don't know to do that when my string is not giving me the hex values when it is printed out. I'm pretty sure it has something to do with hex, but I don't know how to get those int values that print out the correct hex values to be stored into a string without it being changed.
I tried different ways of manipulating this and searched on the web but have not found much of a solution in solving this.
#include <iostream>
#include <string>
#include <sstream>
#include <iterator>
#include <iomanip>
#include <vector>
#include <stdlib.h>
using std::cout;
using std::endl;
using std::string;
using std::hex;
using std::stringstream;
using namespace std;
int main(){
string s2 = "HelloWorld";
cout << "string: " << s2 << endl;
cout << "hexval: ";
vector<int> character; // converting each character to its ascii value
string bytes;
for(int i = 0; i < s2.size(); i++) {
character.push_back(int(s2[i]));
bytes = to_string(character.at(i));
cout << hex << character.at(i) << " ";
cout << bytes << endl;
}
cout << endl;
cout << bytes << endl;
return 0;
}
Here is the output that 'bytes' my string is printing out:
48 72
65 101
6c 108
6c 108
6f 111
57 87
6f 111
72 114
6c 108
64 100
Left is the hexadecimals and right is the string. Two different values. How can I store these hexadecimals that is being converted from a string be stored into a string as a hexadecimal value?
I see 2 different ways:
The first one uses a char array and writes to it with sprintf with %X.
The second way uses a stringstream and streams the int values into it with the hex specifier. You can get the string with the .str() method of stringstream.
#include <iostream>
#include <string>
#include <sstream>
#include <iterator>
#include <iomanip>
#include <vector>
#include <stdlib.h>
using std::cout;
using std::endl;
using std::string;
using std::hex;
using std::stringstream;
using namespace std;
int main(){
string s2 = "HelloWorld";
cout << "string: " << s2 << endl;
string result;
for(int i = 0; i < s2.size(); i++) {
char buffer[20];
sprintf(buffer, "%X ", s2[i]);
result += buffer;
}
cout << "hexval1: " << result << endl;
stringstream res;
for (int val : s2)
res << hex << val << " ";
cout << "hexval2: " << res.str() << endl;
return 0;
}
I have a code which will convert the float value to string, i have written like below
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
float myFloat= 10.80;
std::ostringstream ss;
ss << myFloat;
cout<<"value = " << ss.str();
std::string s(ss.str());
cout<<"value = " << s;
return 0;
}
But the problem is when my value is 10.66 its coming 10.66 but when its 10.80 its coming like 10.8 or when its 10.00 its coming 10 only .
How can i print the complete value
Try this code .
Use the setprecision function with '2' .
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
int main() {
float myFloat= 10.80;
stringstream stream;
stream << fixed << setprecision(2) << myFloat;
string s = stream.str();
cout<<"value = " << s;
return 0;
}
The trailing zeros are only kept if you set either fixed or scientific mode.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double x = 4.2;
cout << fixed << setprecision(2);
cout << x << endl;
return 0;
}
It seems you want something like below.
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;
int main() {
float myFloat= 10.80;
std::ostringstream ss;
ss << fixed << setprecision(2) << myFloat;
cout<<"value = " << ss.str();
std::string s(ss.str());
cout<<"value = " << s;
}
Probably the least complicated way would be to use printf instead of std::cout. There you can specifically specify how many digits are to be displayed.
#include "stdio.h"
printf("%3.2f",myfloat);
where 3 is the # of digits before and 2 the # of digits after the dot, either can be left out. Append '\n' to the string if you want a new line.
EDIT: Ok, I did not know about setprecision(2).
my code:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double A = 100.35;
cout.precision(0);
cout << std::hexfloat << std::fixed << std::left << A << endl;
return 0;
}
Current output:
100
my expected output:
x64
Explanation:
I want to print the hex value of decimal part of double. But I have been unsuccessful in getting this. need help. Any help in this direction will be appreciated.
What you're asking for is simply not possible. std::hex (the output you're looking for) only works for integral arguments, and std::hexfloat uses an undesirable format. You need to cast or round.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
double A = 100.35;
cout.precision(0);
cout << std::hex << std::showbase << std::lround(A) << endl;
return 0;
}
This string operation prints out a double in short-hand, and I can't work out why. Why is this happening, and how can I get the full output like the first line of output?
string myString = "The value is ";
ss.str(""); // stringstream from ealier
ss.clear();
ss << myDouble; // Double with value 0.000014577
myString.append(ss.str());
cout << myDouble << endl;
cout << myString << endl;
$ ./myapp
0.000014577
The value is 1.4577e-05
its default behaviour you should use precision to use fixed precision
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double v = 0.000014577;
cout << fixed << v << endl;
}
Try this:
using std::fixed;
...
ss.setf(fixed);
ss << myDouble;
...
That is because this is the default formatting, you can override it with precision.
Hello
I know it was asked many times but I hadn't found answer to my specific question.
I want to convert only string that contains only decimal numbers:
For example 256 is OK but 256a is not.
Could it be done without checking the string?
Thanks
The simplest way that makes error checking optional that I can think of is this:
char *endptr;
int x = strtol(str, &endptr, 0);
int error = (*endptr != '\0');
In C++ way, use stringstream:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
stringstream sstr;
int a = -1;
sstr << 256 << 'a';
sstr >> a;
if (sstr.failbit)
{
cout << "Either no character was extracted, or the character can't represent a proper value." << endl;
}
if (sstr.badbit)
{
cout << "Error on stream.\n";
}
cout << "Extracted number " << a << endl;
return 0;
}
An other way using c++ style : We check the number of digits to know if the string was valid or not :
#include <iostream>
#include <sstream>
#include <string>
#include <cmath>
int main(int argc,char* argv[]) {
std::string a("256");
std::istringstream buffer(a);
int number;
buffer >> number; // OK conversion is done !
// Let's now check if the string was valid !
// Quick way to compute number of digits
size_t num_of_digits = (size_t)floor( log10( abs( number ) ) ) + 1;
if (num_of_digits!=a.length()) {
std::cout << "Not a valid string !" << std::endl;
}
else {
std::cout << "Valid conversion to " << number << std::endl;
}
}