Generating a new guid string in c++ missing some zeros - c++

I tried using the following code to generate a new guid string of 36 characters, but this sometimes prints only 35 or 34 characters. I suppose this is due to the 'zeros' in the guid, but I am unable to clearly see why. Is there a way I can correct this issue?
#include <string>
#include <sstream>
#include <iostream>
#include <windows.h>
#include <iomanip>
int main()
{
GUID guid;
CoCreateGuid(&guid);
std::ostringstream os;
os.width(8);
os << std::hex << std::setfill('0') << guid.Data1 << '-';
os.width(4);
os << std::hex << std::setfill('0') << guid.Data2 << '-';
os.width(4);
os << std::hex << std::setfill('0') << guid.Data3 << '-';
os.width(2);
os << std::hex << std::setfill('0')
<< static_cast<short>(guid.Data4[0])
<< static_cast<short>(guid.Data4[1])
<< '-'
<< static_cast<short>(guid.Data4[2])
<< static_cast<short>(guid.Data4[3])
<< static_cast<short>(guid.Data4[4])
<< static_cast<short>(guid.Data4[5])
<< static_cast<short>(guid.Data4[6])
<< static_cast<short>(guid.Data4[7]);
std::string s(os.str());
std::cout << s << std::endl;
std::cout << s.length() << std::endl;
}
Sample output:
f6979589-b13c-416d-bf49-1497d99cd88
35
Press any key to continue . . .

You need to directly set field size for any new value you output, like this:
int main()
{
GUID guid;
CoCreateGuid(&guid);
std::ostringstream os;
os.width(8);
os << std::hex << std::setfill('0') << guid.Data1 << '-';
os.width(4);
os << std::hex << std::setfill('0') << guid.Data2 << '-';
os.width(4);
os << std::hex << std::setfill('0') << guid.Data3 << '-';
os.width(2);
os << std::hex << std::setfill('0') << std::setw(2)
<< static_cast<short>(guid.Data4[0]) << std::setw(2)
<< static_cast<short>(guid.Data4[1])
<< '-' << std::setw(2)
<< static_cast<short>(guid.Data4[2]) << std::setw(2)
<< static_cast<short>(guid.Data4[3]) << std::setw(2)
<< static_cast<short>(guid.Data4[4]) << std::setw(2)
<< static_cast<short>(guid.Data4[5]) << std::setw(2)
<< static_cast<short>(guid.Data4[6]) << std::setw(2)
<< static_cast<short>(guid.Data4[7]);
std::string s(os.str());
std::cout << s << std::endl;
std::cout << s.length() << std::endl;
}

Related

Why std::cout print a Octal Number without a 0 in the beginning of the number

If I run this snippet of code
int main()
{
int i = 8;
std::cout << std::oct << i;
}
The console shows 10.
Shouldn't give 010, in C++ Octal Numbers start with 0.
You can use std::showbase for this.
#include <iostream>
int main()
{
int i = 100;
std::cout << "100 without showbase:\n";
std::cout << "decimal: " << std::dec << i << "\n";
std::cout << " hex: " << std::hex << i << "\n";
std::cout << " octal: " << std::oct << i << "\n";
std::cout << "100 with showbase:\n";
std::cout << "decimal: " << std::showbase << std::dec << i << "\n";
std::cout << " hex: " << std::showbase << std::hex << i << "\n";
std::cout << " octal: " << std::showbase << std::oct << i << "\n";
return 0;
}
Output:
100 without showbase:
decimal: 100
hex: 64
octal: 144
100 with showbase:
decimal: 100
hex: 0x64
octal: 0144
Demo

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;
}

How to log a hex string in c++ with ostringstream?

I'm trying to log hex values to an ostringstream, but it's not working. I'm trying:
unsigned char buf[4];
buf[0] = 0;
buf[1] = 1;
buf[2] = 0xab;
buf[3] = 0xcd;
std::ostringstream e1;
e1 << "0x" << std::setfill('0') << std::setw(3) << std::hex << buf[0] << " " << "0x" << std::setfill('0') << std::setw(3) << std::hex << buf[1] << " " << "0x" << std::setfill('0') << std::setw(3) << std::hex << buf[2] << " " << "0x" << std::setfill('0') << std::setw(3) << std::hex << buf[3];
std::cout << e1.str() << std::endl;
I'm expecting to get something like "0x00 0x01 0xab 0xcd" but instead I get "0x00".
I also tried breaking it up like
e1 << "0x" << std::setfill('0') << std::setw(3) << std::hex << buf[0];
e1 << " ";
e1 << "0x" << std::setfill('0') << std::setw(3) << std::hex << buf[1];
e1 << " ";
e1 << "0x" << std::setfill('0') << std::setw(3) << std::hex << buf[2];
e1 << " ";
e1 << "0x" << std::setfill('0') << std::setw(3) << std::hex << buf[3];
but get the same thing.
I assume, that the mainproblem here is the interpretation of char by your stringstream. Try to cast it to int and everything works like charm:
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
int main()
{
unsigned char buf[4];
buf[0] = 0;
buf[1] = 1;
buf[2] = 0xab;
buf[3] = 0xcd;
ostringstream e1;
for (uint i=0; i< sizeof(buf); ++i)
{
e1 << "0x" << std::setw(2) << std::setfill('0') << std::hex << static_cast<int>(buf[i]) << " ";
}
cout << e1.str() << endl;
return 0;
}
This gives you your desired output:
0x00 0x01 0xab 0xcd
The issue is that the characters are not treated as integers in output stream and so the integer manipulators do not affect their output.
Basically ... replace
unsigned char buf[4];
With
unsigned int buf[4];
This works:
e1 << "0x" << std::setfill('0') << std::setw(2) << std::hex << (int)buf[0]
<< " " << "0x" << std::setfill('0') << std::setw(2) << std::hex << (int)buf[1]
<< " " << "0x" << std::setfill('0') << std::setw(2) << std::hex << (int)buf[2]
<< " " << "0x" << std::setfill('0') << std::setw(2) << std::hex << (int)buf[3];
I've added casts to (int) and change setw(2).

Manipulators, C++ what order should I use them in?

I am trying to learn about manipulators...is there a specific order for them?
For ex does std::setw come after or before std::setfill and should they be in separate lines?
There's no specific order, just make sure you include the <iomanip> library.
Example on your setw/setfil question:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << setw(10) << setfill('*');
cout << 123;
}
There is not specific order. But please note this, for example, if you want to use std::left and std::right, or write everything in one line then things can get bit tricky.
For example this will not print expected output (prints just: 7
):
std::cout << std::setw(10) << std::left << 7 << std::setfill('x') << std::endl;
Because you need to set attributes first, then print whatever you want. So all three lines below will work, no matter their places change (prints: xxxxxxxxx7):
std::cout << std::setw(10) << std::setfill('x') << std::right << 7 << std::endl;
std::cout << std::right << std::setw(10) << std::setfill('x') << 7 << std::endl;
std::cout << std::setfill('x') << std::right << std::setw(10) << 7 << std::endl;
And the code below is just to clarify things.
#include <iostream>
#include <iomanip>
int main()
{
std::cout << std::setw(15) << std::setfill('-') << "PRODUCT" << std::setw(15) << std::setfill('-') << "AMOUNT" << std::endl;
std::cout << std::setw(15) << std::setfill('-') << "Brush" << std::setw(15) << std::setfill('-') << 10 << std::endl;
std::cout << std::setw(15) << std::setfill('-') << "Paste" << std::setw(15) << std::setfill('-') << 8 << std::endl << std::endl;
std::cout << std::setw(15) << std::left << std::setfill('-') << "PRODUCT" << std::setw(15) << std::left << std::setfill('-') << "AMOUNT" << std::endl;
std::cout << std::setw(15) << std::left << std::setfill('-') << "Brush" << std::setw(15) << std::left << std::setfill('-') << 10 << std::endl;
std::cout << std::setw(15) << std::left << std::setfill('-') << "Paste" << std::setw(15) << std::left << std::setfill('-') << 8 << std::endl << std::endl;
std::cout << std::setw(15) << std::right << std::setfill('-') << "PRODUCT" << std::setw(15) << std::right << std::setfill('-') << "AMOUNT" << std::endl;
std::cout << std::setw(15) << std::right << std::setfill('-') << "Brush" << std::setw(15) << std::right << std::setfill('-') << 10 << std::endl;
std::cout << std::setw(15) << std::right << std::setfill('-') << "Paste" << std::setw(15) << std::right << std::setfill('-') << 8 << std::endl << std::endl;
return 0;
}

Is there a C++ way to format addresses and pointers with iostream?

I have something like
unsigned x = 16;
unsigned* p = &x;
std::cout << std::hex << std::setw(16) << std::setfill('0') << x << std::endl;
std::cout << std::hex << std::setw(16) << std::setfill('0') << p << std::endl;
output:
0000000000000010
000x7fffc35ba784
ostream::operator<< is overloaded for this? I can write this correctly with C, but I was wondering if there is a proper way to do this with iostream.
Use internal like this:
#include <iostream>
#include <iomanip>
int main()
{
unsigned x = 16;
unsigned* p = &x;
std::cout << std::hex << std::setw(16) << std::setfill('0') << x << std::endl;
std::cout << std::hex << std::setw(16) << std::setfill('0') << p << std::endl;
std::cout << std::internal << std::hex << std::setw(16) << std::setfill('0') << p << std::endl;
}
This gives:
0000000000000010
000x7fffd123c1a4
0x007fffd123c1a4