简体   繁体   English

将uint32转换为const char * C ++

[英]Convert uint32 to const char* C++

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 . 我有一个功能,在需要const char*为参数,但是我有数据是Datum(PostgreSQL) ,其中我转换为Uint32使用DatumGetUint32() function I now need to convert it to const char* in C++ . 我现在需要在C++中将其转换为const char* 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. 当前,数据的格式为Uint32。 The function however takes 'const char*' as the parameter. 但是该函数将“ const char *”作为参数。 So my guess is to convert Uint32 to char and then pass it to the function using a pointer. 所以我的猜测是将Uint32转换为char,然后使用指针将其传递给函数。 But I am unsure how to proceed further. 但是我不确定如何进一步进行。 Hope this helps you to understand better! 希望这可以帮助您更好地理解!

Take a look at boost lexical_cast . 看一下boost lexical_cast I think it will help you. 我认为它将为您提供帮助。

If what you want to do is 如果你想做的是

145 -> "145"

Then sprintf is a pretty standard way of doing the int to char* transition. 然后sprintf是完成从int到char *过渡的相当标准的方法。 Then you just have to cast the newly made char* as a const char* when you call your function. 然后,只需在调用函数时将新创建的char*const char*

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*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM