QDataStream outputs sufficient information to reassemble the result at the other end.
The uint32 values are 4 bytes each in a byte dictated by settings on the stream.
When you stream "INFO-1" you get a 4-byte size (00 00 00 07) followed by the 6 bytes of the string and a zero byte
The bool is streamed as a two byte integer.
The stringstream converts the numeric values to decimal string, so:
#include <cstdint>
#include <iostream>
#include <sstream>
int main(int argc, char **argv) {
std::stringstream str;
str << (uint32_t)0; // "0" in output
str << (uint32_t)0x53486472 // "1397253234" in output i.e. the number in decimal as a string
<< (uint32_t)0 // "0"
<< (uint32_t)1 // "1"
<< (uint32_t)2 // "2"
<< (uint32_t)3 // "3"
<< (uint32_t)4 // "4"
<< (uint32_t)5 // "5"
<< (uint32_t)6 // "6"
<< (uint32_t)7; // "7"
str << (uint32_t)0x54695355 // "1416188757"
<< (char*)"INFO-1"
<< (char*)"869543-1"
<< (char*)"85A-1"
<< (uint16_t)1 // "1"
<< (char*)"C0B3-MV1R-1"
<< (bool)false; // "0"
std::cout << str.str() << std::endl;
return 0;
}
#include <cstdint>
#include <iostream>
#include <sstream>
int main(int argc, char **argv) {
std::stringstream str;
str << (uint32_t)0; // "0" in output
str << (uint32_t)0x53486472 // "1397253234" in output i.e. the number in decimal as a string
<< (uint32_t)0 // "0"
<< (uint32_t)1 // "1"
<< (uint32_t)2 // "2"
<< (uint32_t)3 // "3"
<< (uint32_t)4 // "4"
<< (uint32_t)5 // "5"
<< (uint32_t)6 // "6"
<< (uint32_t)7; // "7"
str << (uint32_t)0x54695355 // "1416188757"
<< (char*)"INFO-1"
<< (char*)"869543-1"
<< (char*)"85A-1"
<< (uint16_t)1 // "1"
<< (char*)"C0B3-MV1R-1"
<< (bool)false; // "0"
std::cout << str.str() << std::endl;
return 0;
}
To copy to clipboard, switch view to plain text mode
outputs:
01397253234012345671416188757INFO-1869543-185A-11C0B3-MV1R-10
01397253234012345671416188757INFO-1869543-185A-11C0B3-MV1R-10
To copy to clipboard, switch view to plain text mode
which is exactly what you receive.
You want something closer to this but you need to think about byte order and what QDataStream is expecting to see:
#include <cstdint>
#include <iostream>
#include <sstream>
template <typename T>
std::stringstream& put ( std::stringstream& str, const T& value )
{
union coercion {
T value;
char data[ sizeof ( T ) ];
};
coercion c;
c.value = value;
str.write ( c.data, sizeof ( T ) );
return str;
}
int main(int argc, char **argv) {
std::stringstream str;
put(str, (uint32_t)0);
put(str, (uint32_t)0x53486472);
put(str, (uint32_t)0);
put(str, (uint32_t)1);
put(str, (uint32_t)2);
put(str, (uint32_t)3);
put(str, (uint32_t)4);
put(str, (uint32_t)5);
put(str, (uint32_t)6);
put(str, (uint32_t)7);
put(str, (uint32_t)0x54695355);
str << (char*)"INFO-1"
<< (char*)"869543-1"
<< (char*)"85A-1";
put(str, (uint16_t)1);
str << (char*)"C0B3-MV1R-1";
put(str, false);
return 0;
}
#include <cstdint>
#include <iostream>
#include <sstream>
template <typename T>
std::stringstream& put ( std::stringstream& str, const T& value )
{
union coercion {
T value;
char data[ sizeof ( T ) ];
};
coercion c;
c.value = value;
str.write ( c.data, sizeof ( T ) );
return str;
}
int main(int argc, char **argv) {
std::stringstream str;
put(str, (uint32_t)0);
put(str, (uint32_t)0x53486472);
put(str, (uint32_t)0);
put(str, (uint32_t)1);
put(str, (uint32_t)2);
put(str, (uint32_t)3);
put(str, (uint32_t)4);
put(str, (uint32_t)5);
put(str, (uint32_t)6);
put(str, (uint32_t)7);
put(str, (uint32_t)0x54695355);
str << (char*)"INFO-1"
<< (char*)"869543-1"
<< (char*)"85A-1";
put(str, (uint16_t)1);
str << (char*)"C0B3-MV1R-1";
put(str, false);
return 0;
}
To copy to clipboard, switch view to plain text mode
Bookmarks