1

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;
2
  • The ASCII code for character "0" is 48. Instead of writing the number 48, it's a good idea to write the more self-documenting '0'. Commented Apr 15, 2015 at 8:26
  • Hi, actually I wanted to use the string for my ANN in opencv. The string must be converted to this cv::Mat data(1,ATTRIBUTES,CV_32F). Is there any way that the data inside the array can be converted to the MAT data? Commented Apr 15, 2015 at 8:30

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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];

2 Comments

Thanks so much ... BTW I had used the string to convert to MAT but got error by using this... Do u have any thought on this?
for(int d=0;d<256;d++) { testing += '0' + pixelValueArray[d]; } cout<<testing; //this is the string cv::Mat data(1,ATTRIBUTES,CV_32F,testing.data());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.