However when I do sizeof(str1) I get 152
That is because you are obtaining the byte size of the ostringstream
object itself. ostringstream
has many data members in it besides just the pointer to the character data. You are looking for the byte size of the actual character data. ostringstream
doesn't provide that size directly.
even thought separately those variables add up to 10 bytes (1+1+8).
The raw variables do, yes. But, by using operator<<
, you are serializing their values to a string
, where each numeric digit becomes its own character in the string, so the total byte size of the final character data will be more like 22, not 10.
When I do sizeof(result) and sizeof(pchar) I get 28 and 4 respectively
Again, because you are obtaining the byte size of the string
object itself (which also has other data members besides just the pointer to the character data), and the byte size of a raw pointer itself respectively. You are looking for the byte size of the character data that the string
internally points to.
result.c_str()
will return a pointer to the actual character data, and result.size()
will give you the proper byte size for that data.
I can only figure my method of doing this is wrong somehow
Using operator<<
is the completely wrong solution in this situation. To put binary data into an ostream
, use its write()
method instead, eg:
uint8_t byte1 = 2;
uint8_t byte2 = 0;
uint64_t bytes = 14829735431805717965;
ostringstream str1;
str1.write(reinterpret_cast<char*>(&byte1), sizeof(byte1));
str1.write(reinterpret_cast<char*>(&byte2), sizeof(byte2));
str1.write(reinterpret_cast<char*>(&bytes), sizeof(bytes));
string result = str1.str();
sendto(..., result.c_str(), result.size(), ...);
Personally, I would suggest using a byte-aligned struct instead:
#pragma pack(push, 1)
struct packet {
uint8_t byte1;
uint8_t byte2;
uint64_t bytes;
};
#pragma pack(pop)
packet p;
p.byte1 = 2;
p.byte2 = 0;
p.bytes = 14829735431805717965;
sendto(..., reinterpret_cast<char*>(&packet), sizeof(packet), ...);
Or, worse case, just use a simple byte array:
char packet[10];
uint8_t byte1 = 2;
uint8_t byte2 = 0;
uint64_t bytes = 14829735431805717965;
memcpy(&packet[0], &byte1, sizeof(byte1));
memcpy(&packet[1], &byte2, sizeof(byte2));
memcpy(&packet[2], &bytes, sizeof(bytes));
/* or:
*reinterpret_cast<uint8_t*>(&packet[0]) = byte1;
*reinterpret_cast<uint8_t*>(&packet[1]) = byte2;
*reinterpret_cast<uint64_t*>(&packet[2]) = bytes;
*/
sendto(..., packet, sizeof(packet), ...);