-1

I have a function that takes in const char* as argument, but the data I have is Datum(PostgreSQL), which I am converting to Uint32 using DatumGetUint32() function. I now need to convert it to const char* in C++. I have looked up online, many different websites but get no conclusive answer. Any help will be appreciated! Thanks in advance.

Edit: This is because I have different functions that I have to pass the data to. Currently, the data is of form Uint32. The function however takes 'const char*' as the parameter. So my guess is to convert Uint32 to char and then pass it to the function using a pointer. But I am unsure how to proceed further. Hope this helps you to understand better!

student001
  • 533
  • 1
  • 7
  • 20
  • How should it be converted? Please be more specific. Do you want the integral number as a string? E.g: `"10035"` for the value 10035? – Sebastian Hoffmann Apr 02 '14 at 11:23
  • So I have this Uint32 called keyval. I want to pass a const char* pointing to it, in a function. – student001 Apr 02 '14 at 13:21
  • A const char* pointing to a Uint32? I'm not sure I get it. – jwav Apr 02 '14 at 13:30
  • This is because I have different functions that I have to pass the data to. Currently, the data is of form Uint32. The function however takes 'const char*' as the parameter. So my guess is to convert Uint32 to char and then pass it to the function using a pointer. But I am unsure how to proceed further. Hope this helps you to understand better! – student001 Apr 02 '14 at 13:37

2 Answers2

2

If what you want to do is

145 -> "145"

Then sprintf is a pretty standard way of doing the int to char* transition. Then you just have to cast the newly made char* as a const char* when you call your function.

Example:

char myString[10] = ""; // 4294967296 is the maximum for Uint32, so 10 characters it is
sprintf(myString, "%d", (long)myUint32); // where 'myUint32' is your Uint32 variable
my_function((const char*)myString); // where 'my_function' is your function requiring a const char*
jwav
  • 615
  • 5
  • 13
1

Take a look at boost lexical_cast. I think it will help you.

Salsa
  • 917
  • 2
  • 13
  • 22
  • +1 because Boost is pretty dandy for a lot of things, but most people don't have it and it can be a pain to install. – jwav Apr 02 '14 at 15:05