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.

justM
  • 3
  • 1
  • 1
  • 4
  • what type of `string`? `std::string`, `char*`, `wchar_t*`, ...? – xtofl May 26 '11 at 12:53
  • 1
    Not enough information. Give a concrete example of your input and you expected output. Also, I don't think you have C++ there, as it does not have a `byte`-datatype. There is also no `memorystream`-class that I know of in C++. – Björn Pollex May 26 '11 at 12:54
  • @justM: Could it be that you are actually using C# or managed C++? – Björn Pollex May 26 '11 at 12:59
  • http://stackoverflow.com/questions/2079912/simpler-way-to-create-a-c-memorystream-from-char-size-t-without-copying-th – Raiv May 26 '11 at 13:00
  • i've re edite my code. i am using c++. please give a concrete answer. – justM May 26 '11 at 13:02

5 Answers5

1

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;
}
Jason
  • 31,834
  • 7
  • 59
  • 78
0

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.

Bhargav
  • 9,869
  • 1
  • 19
  • 29
0

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.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • You _are_ aware that the `back_inserter` will `push_back` repeatedly, which may (and will) cause memory allocations? You can remedy this by `out.reserve( input.size() )`. – xtofl May 26 '11 at 13:23
  • Sure, but why bother? It's easy to add if the profiler shows that the allocations and copy are a bottleneck, but until then, keep it simple. – James Kanze May 27 '11 at 11:09
0

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.

sergio
  • 68,819
  • 11
  • 102
  • 123
0
std::vector<char> bytes(myString.begin(), myString.end());

is a simple solution

O.C.
  • 6,711
  • 1
  • 25
  • 26