[英]Convert uint32 to const char* C++
我有一个功能,在需要const char*
为参数,但是我有数据是Datum(PostgreSQL)
,其中我转换为Uint32
使用DatumGetUint32() function
。 我现在需要在C++
中将其转换为const char*
。 我在网上查找了许多不同的网站,但没有最终的答案。 任何帮助将不胜感激! 提前致谢。
编辑:这是因为我必须将数据传递给不同的功能。 当前,数据的格式为Uint32。 但是该函数将“ const char *”作为参数。 所以我的猜测是将Uint32转换为char,然后使用指针将其传递给函数。 但是我不确定如何进一步进行。 希望这可以帮助您更好地理解!
看一下boost lexical_cast 。 我认为它将为您提供帮助。
如果你想做的是
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*
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.