I want to create a wstring which will have a wstring + NULL + DWORD e.g. L"Text" + NULL + 0x001A. Can I use wstringstream to create such string which has a string ending char "\0" in between ?
hex:54,00,65,00,78,00,74,00,00,00,00,00,1a,00
T e x t \0 00 1A
You can add a null character using the stream's
put()
method:Adding a
DWORD
(what you showed is actually aWORD
) 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 thenput()
each byte as if it were a character:However, note that
wchar_t
is not 2 bytes on every platform, it may be 4 bytes instead. So, usingstd::wstringstream
andstd::wstring
, you are not guaranteed to get the exact output you are looking for on all platforms. you might end up with this instead:If you need consistency across multiple platforms, you can use
std::basic_stringstream<char16_t>
andstd::u16string
instead. Or, usestd::stringstream
andstd::string
(which are based on 1-bytechar
) and just write out all of the individual bytes manually.