justM
justM

Reputation: 3

convert from string to byte[] and add the result into a memorystream

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.

Upvotes: 0

Views: 13035

Answers (5)

Jason
Jason

Reputation: 32530

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;
}

Upvotes: 1

O.C.
O.C.

Reputation: 6829

std::vector<char> bytes(myString.begin(), myString.end());

is a simple solution

Upvotes: 0

sergio
sergio

Reputation: 69027

You can use c_str() on your string to access the string content as a const char*, which should be what you need. Possibly you will need then a cast, but it should be ok, since a char is (usually, and it is on your platform) 1 byte.

Upvotes: 0

James Kanze
James Kanze

Reputation: 153977

It's not clear what you mean by byte: char or unsigned char? And I'm not familiar with your memorystream either. But assuming that you have an std:;string, and you want to append it to an std::vector<unsigned char>, you can use std::copy directly:

std::copy( input.begin(), input.end(), std::back_inserter( out ) );

If some sort of translation is required, std::transform might be usable, as long as it's one char to one byte.

Upvotes: 0

Bhargav
Bhargav

Reputation: 10209

As Space_C0wb0y pointed out, C++ doesn't have a byte datatype. C# has one though. Is that the language you want?

If you're sure that you are dealing with C++, then I guess you'd be using the char datatype which is one byte in size. Also, to convert a std::string to char* (a pointer to a bunch of char values, terminated by \0), you have to call the c_str() function on the variable.

Upvotes: 0

Related Questions