[英]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.