如何在 C++ 中将 unsigned char* 转换为 std::string?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1673445/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:52:25  来源:igfitidea点击:

How to Convert unsigned char* to std::string in C++?

c++bytearraybyte

提问by Unicorn

I have unsigned char*, want to convert it to std::string. Can you please tell me the safest way to do this?

我有unsigned char*,想将其转换为std::string. 你能告诉我最安全的方法吗?

回答by Yacoby

You just needed to cast the unsigned charinto a charas the stringclass doesn't have a constructor that accepts unsigned char:

您只需要将unsigned char转换为 a ,char因为string该类没有接受的构造函数unsigned char

unsigned char* uc;
std::string s( reinterpret_cast< char const* >(uc) ) ;

However, you will need to use the length argument in the constructor if your byte array contains nulls, as if you don't, only part of the array will end up in the string (the array up to the first null)

但是,如果您的字节数组包含空值,则需要在构造函数中使用 length 参数,如果不包含,则只有部分数组会以字符串结尾(数组直到第一个空值)

size_t len;
unsigned char* uc;
std::string s( reinterpret_cast<char const*>(uc), len ) ;

回答by Charles Salvia

BYTE*is probably a typedef for unsigned char*, but I can't say for sure. It would help if you tell us what BYTEis.

BYTE*可能是 的 typedef unsigned char*,但我不能肯定。如果你告诉我们是什么,它会有所帮助BYTE

If BYTE* is unsigned char*, you can convert it to an std::string using the std::string range constructor, which will take two generic Iterators.

如果 BYTE* 是 unsigned char*,则可以使用 std::string 范围构造函数将其转换为 std::string,这将采用两个通用迭代器。

const BYTE* str1 = reinterpret_cast<const BYTE*> ("Hello World");
int len = strlen(reinterpret_cast<const char*>(str1));
std::string str2(str1, str1 + len);

That being said, are you sure this is a good idea? If BYTEis unsigned charit may contain non-ASCII characters, which can include NULLs. This will make strlengive an incorrect length.

话虽如此,你确定这是个好主意吗?如果BYTE是,unsigned char它可能包含非 ASCII 字符,其中可以包含 NULL。这将导致strlen错误的长度。

回答by spoulson

BYTE *str1 = "Hello World";
std::string str2((char *)str1);  /* construct on the stack */

Alternatively:

或者:

std::string *str3 = new std::string((char *)str1); /* construct on the heap */
cout << &str3;
delete str3;

回答by Satbir

BYTE is nothing but typedef unsigned char BYTE;

BYTE不过是 typedef unsigned char BYTE;

You can easily use any of below constructors

您可以轻松使用以下任何构造函数

string ( const char * s, size_t n );
string ( const char * s );

回答by user2195463

If has access to CryptoPP

如果可以访问 CryptoPP

Readable Hex String to unsigned char

可读的十六进制字符串到无符号字符

std::string& hexed = "C23412341324AB";
uint8_t      buffer[64] = {0};
StringSource ssk(hexed, true,
            new HexDecoder(new ArraySink(buffer,sizeof(buffer))));

And back

然后回来

std::string hexed;
uint8_t val[32]  = {0};
StringSource ss(val, sizeof(val), true,new HexEncoder(new StringSink(hexed));
// val == buffer