c++char-pointer

How to convert uint64_t to const char * in C++?


I have one method which accepts const char * as shown below -

bool get_data(const char* userid) const;

Now below is my for loop in which I need to call get_data method by passing const char *. Currently below for loop uses uint64_t. I need to convert this uint64_t to const char * and then pass to get_data method.

for (TmpIdSet::iterator it = userids.begin(); it != userids.end(); ++it) {
    // .. some code
    // convert it to const char *
    mpl_files.get_data(*it)
}

Here TmpIdSet is typedef std::set<uint64_t> TmpIdSet;

So my question is how should I convert uint64_t in the above code to const char *?


Solution

  • One way would be to convert it first to a std::string using std::to_string and then access the raw data using the std::string::c_str() member function:

    #include <string>
    
    ....
    
    uint64_t integer = 42;
    std::string str = std::to_string(integer);
    mpl_files.get_data(str.c_str());