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









share|improve this question






















  • 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

















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









share|improve this question






















  • 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















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









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 20:52









Alex

1




1












  • 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




















  • 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


















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














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.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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.






    share|improve this answer



























      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.






      share|improve this answer

























        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.






        share|improve this answer














        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 8 at 21:39

























        answered Nov 8 at 21:17









        Remy Lebeau

        326k18244430




        326k18244430






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            How to pass form data using jquery Ajax to insert data in database?

            National Museum of Racing and Hall of Fame

            Guess what letter conforming each word