将非空终止的无符号字符数组复制到 std::string

发布于 2024-10-11 21:34:26 字数 465 浏览 18 评论 0原文

如果数组是空终止,这将非常简单:

unsigned char u_array[4] = { 'a', 's', 'd', '\0' };
std::string str = reinterpret_cast<char*>(u_array);
std::cout << "-> " << str << std::endl;

但是,我想知道复制非空终止无符号字符数组的最合适方法是什么,例如以下内容:

unsigned char u_array[4] = { 'a', 's', 'd', 'f' };

转换为 std::string

有没有办法在不迭代无符号字符数组的情况下做到这一点?

谢谢大家。

If the array was null-terminated this would be pretty straight forward:

unsigned char u_array[4] = { 'a', 's', 'd', '\0' };
std::string str = reinterpret_cast<char*>(u_array);
std::cout << "-> " << str << std::endl;

However, I wonder what is the most appropriate way to copy a non null-terminated unsigned char array, like the following:

unsigned char u_array[4] = { 'a', 's', 'd', 'f' };

into a std::string.

Is there any way to do it without iterating over the unsigned char array?

Thank you all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

杯别 2024-10-18 21:34:26

std::string 有一个 构造函数采用一对迭代器,unsigned char 可以(以实现定义的方式)转换为 char,因此可以工作。不需要reinterpret_cast

unsigned char u_array[4] = { 'a', 's', 'd', 'f' };

#include <string>
#include <iostream>
#include <ostream>

int main()
{
    std::string str( u_array, u_array + sizeof u_array / sizeof u_array[0] );
    std::cout << str << std::endl;
    return 0;
}

当然,“数组大小”模板函数比 sizeof 计算更稳健。

std::string has a constructor that takes a pair of iterators and unsigned char can be converted (in an implementation defined manner) to char so this works. There is no need for a reinterpret_cast.

unsigned char u_array[4] = { 'a', 's', 'd', 'f' };

#include <string>
#include <iostream>
#include <ostream>

int main()
{
    std::string str( u_array, u_array + sizeof u_array / sizeof u_array[0] );
    std::cout << str << std::endl;
    return 0;
}

Of course an "array size" template function is more robust than the sizeof calculation.

一桥轻雨一伞开 2024-10-18 21:34:26

好吧,显然 std::string 有一个可以在这种情况下使用的构造函数

std::string str(reinterpret_cast<char*>(u_array), 4);

Well, apparently std::string has a constructor that could be used in this case:

std::string str(reinterpret_cast<char*>(u_array), 4);
倥絔 2024-10-18 21:34:26

当构造字符串而不指定其大小时,构造函数将迭代字符数组并查找空终止符,即 '\0' 字符。如果没有该字符,则必须显式指定长度,例如:

// --*-- C++ --*--

#include <string>
#include <iostream>


int
main ()
{
    unsigned char u_array[4] = { 'a', 's', 'd', 'f' };
    std::string str (reinterpret_cast<const char *> (u_array),
                     sizeof (u_array) / sizeof (u_array[0]));
    std::cout << "-> " << str << std::endl;
}

When constructing a string without specifying its size, constructor will iterate over a a character array and look for null-terminator, which is '\0' character. If you don't have that character, you have to specify length explicitly, for example:

// --*-- C++ --*--

#include <string>
#include <iostream>


int
main ()
{
    unsigned char u_array[4] = { 'a', 's', 'd', 'f' };
    std::string str (reinterpret_cast<const char *> (u_array),
                     sizeof (u_array) / sizeof (u_array[0]));
    std::cout << "-> " << str << std::endl;
}
伪装你 2024-10-18 21:34:26

std::string 有一个名为 allocate 的方法。您可以使用 char * 和大小。

http://www.cplusplus.com/reference/string/string/assign/

std::string has a method named assign. You can use a char * and a size.

http://www.cplusplus.com/reference/string/string/assign/

傲鸠 2024-10-18 21:34:26

这应该可以做到:

std::string s(u_array, u_array+sizeof(u_array)/sizeof(u_array[0]));

This should do it:

std::string s(u_array, u_array+sizeof(u_array)/sizeof(u_array[0]));
白云悠悠 2024-10-18 21:34:26

您可以使用此 std::string 构造函数:

string ( const char * s, size_t n );

因此在您的示例中:

std::string str(u_array, 4);

You can use this std::string constructor:

string ( const char * s, size_t n );

so in your example:

std::string str(u_array, 4);
握住你手 2024-10-18 21:34:26

您可以创建一个指向第一个字符的字符指针,另一个指向最后一个字符的字符指针,并使用这两个指针作为迭代器进行构造。因此:

std::string str(&u_array[0], &u_array[0] + 4);

You can create a character pointer pointing to the first character, and another pointing to one-past-the-last, and construct using those two pointers as iterators. Thus:

std::string str(&u_array[0], &u_array[0] + 4);
我要还你自由 2024-10-18 21:34:26

当字符串本身包含空字符并且您随后尝试打印该字符串时,仍然存在问题:

char c_array[4] = { 'a', 's', 'd', 0 };

std::string toto(array,4);
cout << toto << endl;  //outputs a 3 chars and a NULL char

但是......

cout << toto.c_str() << endl; //will only print 3 chars.

当您只想放弃可爱并使用裸C时,就会出现这样的问题。

There is a still a problem when the string itself contains a null character and you try to subsequently print the string:

char c_array[4] = { 'a', 's', 'd', 0 };

std::string toto(array,4);
cout << toto << endl;  //outputs a 3 chars and a NULL char

However....

cout << toto.c_str() << endl; //will only print 3 chars.

Its times like these when you just want to ditch cuteness and use bare C.

落日海湾 2024-10-18 21:34:26

尽管问题是如何“将非空终止的 unsigned char 数组 [...] 复制到 std::string”,但我注意到在给定的示例中该字符串仅用作 std::cout 的输入。

在这种情况下,当然您可以完全避免使用字符串,而只需

std::cout.write(u_array, sizeof u_array);
std::cout << std::endl;

这样做我认为可以解决OP试图解决的问题。

Although the question was how to "copy a non null-terminated unsigned char array [...] into a std::string", I note that in the given example that string is only used as an input to std::cout.

In that case, of course you can avoid the string altogether and just do

std::cout.write(u_array, sizeof u_array);
std::cout << std::endl;

which I think may solve the problem the OP was trying to solve.

我一直都在从未离去 2024-10-18 21:34:26

尝试:

std::string str;
str.resize(4);
std::copy(u_array, u_array+4, str.begin());

Try:

std::string str;
str.resize(4);
std::copy(u_array, u_array+4, str.begin());
|煩躁 2024-10-18 21:34:26

std::string 有一个构造函数,采用 char 数组和长度。

unsigned char u_array[4] = { 'a', 's', 'd', 'f' };
std::string str(reinterpret_cast<char*>(u_array), sizeo(u_array));

std::string has a constructor taking an array of char and a length.

unsigned char u_array[4] = { 'a', 's', 'd', 'f' };
std::string str(reinterpret_cast<char*>(u_array), sizeo(u_array));
余生一个溪 2024-10-18 21:34:26

呃,为什么要选演员?

 std::string str(u_array, u_array + sizeof(u_array));

完毕。

Ew, why the cast?

 std::string str(u_array, u_array + sizeof(u_array));

Done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文