C++ 将字符数组缓冲区转换为字符串的好方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/896103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Good methods for converting char array buffers to strings?
提问by Newton Falls
I am relatively new to C++. Recent assignments have required that I convert a multitude of char buffers (from structures/sockets, etc.) to strings. I have been using variations on the following but they seem awkward. Is there a better way to do this kind of thing?
我对 C++ 比较陌生。最近的任务要求我将大量字符缓冲区(从结构/套接字等)转换为字符串。我一直在使用以下变体,但它们看起来很尴尬。有没有更好的方法来做这种事情?
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
char* bufferToCString(char *buff, int buffSize, char *str)
{
memset(str, 'std::string bufferToString(char* buffer, int bufflen)
{
std::string ret(buffer, bufflen);
return ret;
}
', buffSize + 1);
return(strncpy(str, buff, buffSize));
}
string& bufferToString(char* buffer, int bufflen, string& str)
{
char temp[bufflen];
memset(temp, 'char buff[4] = {'a', 'b', 'c', 'd'};
cout << std::string(&buff[0], 4);
', bufflen + 1);
strncpy(temp, buffer, bufflen);
return(str.assign(temp));
}
int main(int argc, char *argv[])
{
char buff[4] = {'a', 'b', 'c', 'd'};
char str[5];
string str2;
cout << bufferToCString(buff, sizeof(buff), str) << endl;
cout << bufferToString(buff, sizeof(buff), str2) << endl;
}
回答by Simon Parker
Given your input strings are not null terminated, you shouldn't use str... functions. You also can't use the popularly used std::string
constructors. However, you can use this constructor:
鉴于您的输入字符串不是空终止,您不应该使用 str... 函数。您也不能使用常用的std::string
构造函数。但是,您可以使用此构造函数:
std::string str(buffer, buflen)
: it takes a char*
and a length. (actually const char*
and length)
std::string str(buffer, buflen)
: 它需要一个char*
和一个长度。(实际const char*
和长度)
I would avoid the C string version. This would give:
我会避免使用 C 字符串版本。这将给出:
cout << std::string(&buff[0], &buff[4]); // end is last plus one
If you really must use the C-string
version, either drop a 0
at the bufflen
position (if you can) or create a buffer of bufflen+1
, then memcpy
the buffer into it, and drop a 0
at the end (bufflen
position).
如果您确实必须使用该C-string
版本,请0
在bufflen
位置(如果可以)或创建一个缓冲区bufflen+1
,然后memcpy
将缓冲区放入其中,并0
在末尾(bufflen
位置)放置一个。
回答by David Dolson
If the data buffer may have null ('\0') characters in it, you don't want to use the null-terminated operations.
如果数据缓冲区中可能包含空 ('\0') 字符,则您不想使用以空字符结尾的操作。
You can either use the constructor that takes char*, length.
您可以使用带有 char*, length 的构造函数。
my_str.c_str();
Or you can use the constructor that takes a range:
或者您可以使用带有范围的构造函数:
string my_str1 ("test");
char test[] = "test";
string my_str2 (test);
Do NOT use the std::string(buff) constructor with the buff[] array above, because it is not null-terminated.
不要将 std::string(buff) 构造函数与上面的 buff[] 数组一起使用,因为它不是以 null 结尾的。
回答by Dan Olson
std::string to const char*:
std::string 到 const char*:
string my_str3 = "test";
char* to std::string:
字符 * 到 std::string:
std::string buf2str(const char* buffer)
{
return std::string(buffer);
}
or even
甚至
std::string mystring(buffer);
回答by rlbond
string ( const char * s, size_t n );
Or just
要不就
string ( const char * s, size_t n );
回答by rlbond
Use string constructor that takes the size:
使用采用大小的字符串构造函数:
cout << std::string(buff, sizeof(buff)) << endl;
Content is initialized to a copy of the string formed by the first n characters in the array of characters pointed by s.
memcpy(str, buff, buffSize); str[bufSize] = 0; // not buffSize+1, because C indexes are 0-based.
内容被初始化为由 s 指向的字符数组中的前 n 个字符形成的字符串的副本。
std::string bufferToString(char* buffer, int bufflen)
{
return std::string(buffer, bufflen);
}
http://www.cplusplus.com/reference/string/string/string/
http://www.cplusplus.com/reference/string/string/string/
Non-null-terminated buffer to C string:
非空终止缓冲区到 C 字符串:
std::string bufferToString(char* buffer)
{
return std::string(buffer);
}
回答by stefanB
The method needs to know the size of the string. You have to either:
该方法需要知道字符串的大小。您必须:
- in case of char* pass the length to method
- in case of char* pointing to null terminating array of characters you can use everything up to null character
- for char[] you can use templates to figure out the size of the char[]
- 在 char* 的情况下,将长度传递给方法
- 在 char* 指向空终止字符数组的情况下,您可以使用最多空字符的所有内容
- 对于 char[],您可以使用模板来确定 char[] 的大小
1) example - for cases where you're passing the bufflen:
1) 示例 - 对于您传递 bufflen 的情况:
template <typename T, size_t N>
std::string tostr(T (&array)[N])
{
return std::string(array, N);
}
Usage:
char tstr[] = "Test String";
std::string res = tostr(tstr);
std::cout << res << std::endl;
2) example - for cases where buffer is points to null terminated array of characters:
2) 示例 - 对于缓冲区指向空终止字符数组的情况:
std::string(buffer, bufflen);
std::string(buffer);
3) example - for cases where you pass char[]:
3) 示例 - 对于您传递 char[] 的情况:
##代码##For the first 2 cases you don't actually have to create new method:
对于前两种情况,您实际上不必创建新方法:
##代码##回答by Karthik Mohan
string value (reinterpret_cast(buffer), length);
字符串值(reinterpret_cast(缓冲区),长度);