Thursday, July 12, 2012

C/C++ Programming Notes 2: int to string conversion

For computer vision related experiments, output experiments results on top of the images is usually a convenient way to check the results, such as Person Id, location pixel values and so on.

However, most likely, the putText like functions, prefer "string" as the input type. Then how are we going to convert the digital (int/double) data type to string, is a small but important question. 

In Matlab: 
str = ['PersonId ' num2str(id)];

In C++
#include <iostream>
#include <sstream>

using namespace std;
int main(int argc, char *argv[]) {


    std::ostringstream convert;
    int id = 5;
    string txt;
    convert << id;
    txt = "PersonId " + convert.str();
    cout << txt << endl;
    return 0;
}

No comments:

Post a Comment

prettify