如何将数组中的数据转换为字符串 c++

How to convert data in array to string c++

本文关键字:转换 字符串 c++ 数据 数组      更新时间:2023-10-16

我有包含 1 或 0 的数组。我想附加它并成为一行字符串。目前我失败了。这是我的代码。请帮忙,因为我无法完成它。每次我将最终结果加载到控制台时,只有笑脸,而不是 1 或 0。请帮忙

int pixelValueArray[256];
String testing;
for(int d=0;d<256;d++)
{
    testing.append(1,pixelValueArray[d]);
}
cout<<testing;

Std 提供了函数 std::to_string()(自 c++11 以来)将 int 等数据类型转换为 std::string: http://en.cppreference.com/w/cpp/string/basic_string/to_string 。也许这可以帮助你。

整数

的 ASCII 值由 '0' + digit 给出。

for(int i = 0; i < 256; i++)
    testing.append(1, '0' + pixelValueArray[i]);

或者您可以使用更简单的+=

for(int i = 0; i < 256; i++)
    testing += '0' + pixelValueArray[i];