将uint32转换为const char * C ++

时间:2014-04-02 11:17:14

标签: c++ postgresql uint32

我有一个以const char*为参数的函数,但我的数据是Datum(PostgreSQL),我使用Uint32将其转换为DatumGetUint32() function。我现在需要将其转换为const char*中的C++。我在网上查了很多不同的网站,但没有得到确凿的答案。任何帮助将不胜感激!提前致谢。

编辑:这是因为我有不同的功能,我必须将数据传递给。目前,数据的格式为Uint32。然而,该功能需要' const char *'作为参数。所以我的猜测是将Uint32转换为char,然后使用指针将其传递给函数。但我不确定如何继续前进。希望这有助于您更好地理解!

2 个答案:

答案 0 :(得分:1)

看看boost lexical_cast。我认为它会对你有帮助。

答案 1 :(得分:1)

如果你想做的是

145 -> "145"

然后sprintf是执行int到char *转换的一种非常标准的方法。然后,当您调用函数时,您只需将新创建的char*投射为const char*

示例:

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*