1

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 *?

john
  • 11,311
  • 40
  • 131
  • 251
  • 3
    What do you mean by "converting a number to a pointer"? How do you convert kittens into a happy thought? – Kerrek SB May 29 '14 at 23:56
  • have you tried `ltoa()`? – Red Alert May 29 '14 at 23:56
  • Do you actually want the 64 bit integer converted to a string and then passed or do you simply want to change the type (which would probably be undefined behaviour)? – Jerry Jeremiah May 29 '14 at 23:57
  • @JerryJeremiah I want the 64 bit integer converted to a string and then passed. Actually C++ is not my first language so don't know much about the predefined functions. I did some search and couldn't find the solution as well. – john May 29 '14 at 23:59
  • A UUID is 128 bit, how are you fitting it into an `uint64_t`? – Matteo Italia May 29 '14 at 23:59
  • sorry that uuid I have is not 128 bit, it is just a variable name in my case. Sorry for the confusion – john May 30 '14 at 00:00
  • @JerryJeremiah it's well-define to alias a type by a char type – M.M May 30 '14 at 00:26

3 Answers3

3

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());
Martin Drozdik
  • 12,742
  • 22
  • 81
  • 146
1
 mpl_files.get_data( std::to_string( *it ).c_str() )

Include <string> header. For g++ specify -std=c++11.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
-1

You can simply cast as const char *:

uint64_t integer;
const char * newpointer = (const char *)integer;