You can add a null character using the stream's put()
method:
my_wstringstream.put(0);
Adding a DWORD
(what you showed is actually a WORD
) is trickier. You can't use the <<
operator, that will format the numeric value into a text representation, which is not what you are asking for. You would have to instead break up the value into its individual bytes, and then put()
each byte as if it were a character:
my_wstringstream.put(0).put(0x00).put(0x1A);
However, note that wchar_t
is not 2 bytes on every platform, it may be 4 bytes instead. So, using std::wstringstream
and std::wstring
, you are not guaranteed to get the exact output you are looking for on all platforms. you might end up with this instead:
hex:54,00,00,00,65,00,00,00,78,00,00,00,74,00,00,00,00,00,00,00,00,00,00,00,1a,00,00,00
T e x t \0 00 1A
If you need consistency across multiple platforms, you can use std::basic_stringstream<char16_t>
and std::u16string
instead. Or, use std::stringstream
and std::string
(which are based on 1-byte char
) and just write out all of the individual bytes manually.