How to convert a std::string to const char* or char*?

How to convert a std::string to const char* or char*?

1.

If you just want to pass a std::string to a function that needs const char* you can use

std::string str;
const char * c = str.c_str();
If you want to get a writable copy, like char *, you can do that with this:

std::string str;
char * writable = new char[str.size() + 1];
std::copy(str.begin(), str.end(), writable);
writable[str.size()] = ''; // don't forget the terminating 0

// don't forget to free the string after finished using it
delete[] writable;
Edit: Notice that the above is not exception safe. If anything between the new call and the delete call throws, you will leak memory, as nothing will call delete for you automatically. There are two immediate ways to solve this.

boost::scoped_array

boost::scoped_array will delete the memory for you upon going out of scope:

std::string str;
boost::scoped_array<char> writable(new char[str.size() + 1]);
std::copy(str.begin(), str.end(), writable.get());
writable[str.size()] = ''; // don't forget the terminating 0

// get the char* using writable.get()

// memory is automatically freed if the smart pointer goes 
// out of scope
std::vector

This is the standard way (does not require any external library). You use std::vector, which completely manages the memory for you.

std::string str;
std::vector<char> writable(str.begin(), str.end());
writable.push_back('');

// get the char* using &writable[0] or &*writable.begin()
shareimprove this answer

原文地址:http://stackoverflow.com/questions/347949/how-to-convert-a-stdstring-to-const-char-or-char

2.


1
down vote
accepted
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;
}
shareimprove this answer

原文地址:http://stackoverflow.com/questions/6138771/convert-from-string-to-byte-and-add-the-result-into-a-memorystream

原文地址:https://www.cnblogs.com/ZhYQ-Note/p/6061682.html