简体   繁体   中英

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. 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

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 . Maybe this can help you.

The ASCII values for integer digits are given by '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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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