简体   繁体   English

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

[英]How to convert data in array to string c++

I have arrays which contains either 1 or 0. I would like to append it and become one line string. 我有包含1或0的数组。我想将它追加并成为一个行字符串。 At the moment I had failed doing so. 目前我没有这样做。 here is my code. 这是我的代码。 Please help because I could not manage to complete it. 请帮助,因为我无法完成它。 Everytime I load the final result to the console, only smiley faces and not 1 or 0. Please help 每次我将最终结果加载到控制台时,只有笑脸而不是1或0.请帮忙

int pixelValueArray[256];
String testing;

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

cout<<testing;

Std provides the function std::to_string() (since c++11) to convert Datatypes like int to std::string: http://en.cppreference.com/w/cpp/string/basic_string/to_string . Std提供函数std :: to_string()(自c ++ 11开始)将数据类型转换为std :: string: http//en.cppreference.com/w/cpp/string/basic_string/to_string Maybe this can help you. 也许这可以帮到你。

The ASCII values for integer digits are given by '0' + digit . 整数位的ASCII值由'0' + digit

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

Or you could use the simpler += 或者你可以使用更简单的+=

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM