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 5942313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:14:08+00:00 2026-05-22T16:14:08+00:00

how can i convert a string into a byte[] and them add the result

  • 0

how can i convert a string into a byte[] and them add the result using in a stream. THX. I am working in c++ code, ubuntu.

EDIT:
i have a std::string as a string value.
I need a method that takes as an input a std::string value and returns as an output the stream value.

  • 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-22T16:14:09+00:00Added an answer on May 22, 2026 at 4:14 pm

    You can get a pointer to the string buffer by simply calling std::string.c_str(). That will return a const char* (where char is an int8_t) that you can effectively use as a byte[]. Keep in mind though that the pointer returned is pointing to memory managed by the string object, so if you change anything in the original string class, you will invalidate the pointer. Also since it’s a pointer to a const char, you shouldn’t change any values in the buffer. So if you need more permanent memory, or need a buffer you can modify, a better way to accomplish your goal would be to-do (using gcc, which shouldn’t be a problem since you’re on Ubuntu):

    std::string my_string;
    char string_array[my_string.length() + 1];
    strcpy(string_array, my_string.c_str());
    

    Now use the string_array as your memory buffer.

    If you need to return the buffer from a function, you’re going to have to allocate the buffer on the heap and return a pointer. That also means you’re going to have to call delete [] on the pointer as well after you’re done with it, or else you’re going to end up with a memory leak. So you could do the following:

    #include <string>
    #include <cstring>
    
    char* return_buffer(const std::string& string)
    {
       char* return_string = new char[string.length() + 1];
       strcpy(return_string, string.c_str());
    
       return return_string;
    }
    
    //now use in code
    int main()
    {
        std::string some_string = "Stuff";
        char* buffer = return_buffer(some_string);
    
        //...do something with buffer
    
        //...after you're done with the buffer to prevent memory leak
        delete [] buffer;
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can we convert a hex string to a byte array using a built-in function
How can I convert a string of bytes into an int in python? Say
Can I convert a string representing a boolean value (e.g., 'true', 'false') into an
I am using the below function in Java to convert an encrypted String into
I have a hex string,st=9e. I need to convert it into byte and insert
How can I convert my Map<Integer, String > to byte[] , and then write
can someboody please help me convert these strings/ints into a usable font thing? it
can I convert a string to a html object? like: string s = '<div
How can I convert a string to a date time object in javascript by
How can I convert a string 08:00 to a DateTime datatype? Im trying to

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.