In C++, how can I use wstringstream to combine/concat wstring + NULL + DWORD
up vote
-2
down vote
favorite
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 "" in between ?
hex:54,00,65,00,78,00,74,00,00,00,00,00,1a,00
T e x t 00 1A
c++ null stringstream wstring dword
add a comment |
up vote
-2
down vote
favorite
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 "" in between ?
hex:54,00,65,00,78,00,74,00,00,00,00,00,1a,00
T e x t 00 1A
c++ null stringstream wstring dword
Did you trymy_wstringstream << my_wstring << L'' << my_dword;?
– NathanOliver
Nov 8 at 20:54
Yes! It will ignore the L''
– Alex
Nov 8 at 21:00
@Alex no,<< L''will not ignore a null character.<< L""would, though.
– Remy Lebeau
Nov 8 at 21:20
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
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 "" in between ?
hex:54,00,65,00,78,00,74,00,00,00,00,00,1a,00
T e x t 00 1A
c++ null stringstream wstring dword
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 "" in between ?
hex:54,00,65,00,78,00,74,00,00,00,00,00,1a,00
T e x t 00 1A
c++ null stringstream wstring dword
c++ null stringstream wstring dword
asked Nov 8 at 20:52
Alex
1
1
Did you trymy_wstringstream << my_wstring << L'' << my_dword;?
– NathanOliver
Nov 8 at 20:54
Yes! It will ignore the L''
– Alex
Nov 8 at 21:00
@Alex no,<< L''will not ignore a null character.<< L""would, though.
– Remy Lebeau
Nov 8 at 21:20
add a comment |
Did you trymy_wstringstream << my_wstring << L'' << my_dword;?
– NathanOliver
Nov 8 at 20:54
Yes! It will ignore the L''
– Alex
Nov 8 at 21:00
@Alex no,<< L''will not ignore a null character.<< L""would, though.
– Remy Lebeau
Nov 8 at 21:20
Did you try
my_wstringstream << my_wstring << L'' << my_dword;?– NathanOliver
Nov 8 at 20:54
Did you try
my_wstringstream << my_wstring << L'' << my_dword;?– NathanOliver
Nov 8 at 20:54
Yes! It will ignore the L''
– Alex
Nov 8 at 21:00
Yes! It will ignore the L''
– Alex
Nov 8 at 21:00
@Alex no,
<< L'' will not ignore a null character. << L"" would, though.– Remy Lebeau
Nov 8 at 21:20
@Alex no,
<< L'' will not ignore a null character. << L"" would, though.– Remy Lebeau
Nov 8 at 21:20
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
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 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.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
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 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.
add a comment |
up vote
1
down vote
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 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.
add a comment |
up vote
1
down vote
up vote
1
down vote
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 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.
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 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.
edited Nov 8 at 21:39
answered Nov 8 at 21:17
Remy Lebeau
326k18244430
326k18244430
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53215960%2fin-c-how-can-i-use-wstringstream-to-combine-concat-wstring-null-dword%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Did you try
my_wstringstream << my_wstring << L'' << my_dword;?– NathanOliver
Nov 8 at 20:54
Yes! It will ignore the L''
– Alex
Nov 8 at 21:00
@Alex no,
<< L''will not ignore a null character.<< L""would, though.– Remy Lebeau
Nov 8 at 21:20