Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5846855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:38:10+00:00 2026-05-22T12:38:10+00:00

If this is valid: unsigned char buffer[] = { 0x01, 0x02, 0x03, 0x04 };

  • 0

If this is valid:

unsigned char buffer[] = {
 0x01, 0x02, 0x03, 0x04
};

Does this apply to std::string as well, e.g.

std::string buffer = {
 0x01, 0x02, 0x03, 0x04
};

If not, how do I insert such values?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:38:10+00:00Added an answer on May 22, 2026 at 12:38 pm

    Not in C++03, but you can do this in C++011:

     std::string buffer = { 0x01, 0x02, 0x03, 0x04 }; //C++011 ONLY
    

    Demo : http://www.ideone.com/1cOuX. In the demo, I used printable characters ('A', 'B' etc) just for the sake of demonstration.


    In C++03, since the solution given by @andrewdski is admittedly ugly, you can do this instead:

        unsigned char values[] = {0x01, 0x02, 0x03, 0x04 };
        std::string buffer(values, values + sizeof(values));
    

    which is a bit cleaner approach. And if you want to add more values later on, then you can do this:

    unsigned char more_values[] = {0x05, 0x06, 0x07, 0x08 };
    buffer.insert(buffer.end(), more_values, more_values+ sizeof(more_values));
    

    And if you want to add values from other std::string, then you can do this:

    //add values from s to buffer
    buffer.insert(buffer.end(), s.begin(), s.end());
    

    which is same as:

    buffer += s; //cool 
    

    By the way, you can write a small utility called join that can do that, in addition to other interesting thing:

    std::string s = join() + 'A' + 'B' + 'C' + 'D';
    

    You can even mix different types in a single join statement as:

    std::string s = join() + 'A' + 'B' + 'C' + 'D' + "haha" + 9879078;
    

    This is impossible with {} approach even in C++011.

    The utility and a complete demo of it is shown below:

    #include <iostream>
    #include <string>
    #include <sstream>
    
    struct join
    {
       std::stringstream ss;
       template<typename T>
       join & operator+(const T &data)
       {
            ss << data;
            return *this;
       }
       operator std::string() { return ss.str(); }
    };
    
    int main() {
            std::string s1 = join() + 'A' + 'B' + 'C' + 'D';
            std::cout << s1 << std::endl;
            std::string s2 = join() + 'A' + 'B' + 'C' + 'D' + "haha" + 9879078;
            std::cout << s2 << std::endl;
    }
    

    Output:

    ABCD
    ABCDhaha9879078
    

    Online Demo : http://www.ideone.com/3Y7pB

    However, this utility requires casting with the specific values which you want to insert to the string as:

    std::string buffer=join()+ (char)0x01 +(char)0x02 + (char)0x03 + (char)0x04;
    

    No so cool, admittedly. The cast is needed otherwise each value will be treated as int type which you don’t want. So I would advocate the cleaner approach shown earlier. But this utility can help you in some other scenario. And its good to experiment with C++ operator and features, sometimes. 😀

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is this valid C++ (e.g. not invoking UB) and does it achieve what I
When this is valid: string s4 = H e l l o; string[] arr
CREATE TABLE profile_category ( id mediumint UNSIGNED NOT NULL AUTO_INCREMENT, pc_name char(255) NOT NULL,
Is this valid and correct? RewriteRule ^myOldPage.html$ /index.php#info [R] I'm specifically interested about the
Is this valid: $_SESSION['pictures']['rateAlbum']['_POST'] = $_POST; I want to save all of the POST
As you know the dash introduces a comment how can I make this valid?
Quick question, how can I make this valid : if($this->datos->bathrooms == 1½){$select1 = JText::_(
Is this a valid jQuery syntax :$('#id')[0];
Is this a valid way to do performance analysis? I want to get nanosecond
is this a valid JQuery usage pattern to : <script type=text/javascript> $(body).prepend('<input type=hidden id=error

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.