c – 将long转换为char * const

c – 将long转换为char * const,第1张

概述在C中将long转换为char * const的正确方法是什么? 编辑: long l = pthread_self();ThirdPartyFunction("Thread_Id_"+l); //Need to do thisThirdPartyFunction(char* const identifierString){} 编辑: 在C中将整数转换为字符串的“正确”方法是使用字符串流 在C中将long转换为char * const的正确方法是什么?

编辑:

long l = pthread_self();ThirdPartyFunction("Thread_ID_"+l); //Need to do thisThirdPartyFunction(char* const IDentifIErString){}
解决方法 编辑:
在C中将整数转换为字符串的“正确”方法是使用字符串流.例如:

#include <sstream>std::ostringstream oss;oss << "Thread_ID_" << l;ThirdPartyFunction(oss.str().c_str());

现在,这可能不是“最快”的方式(流有一些开销),但它简单,可读,更重要的是,安全.

以下老答案

取决于你的意思“转换”.

要将long的内容转换为指针:

char * const p = reinterpret_cast<char * const>(your_long);

要“看到”长长的一系列字符:

char * const p = reinterpret_cast<char * const>(&your_long);

要将long转换为字符串:

std::ostringstream oss;oss << your_long;std::string str = oss.str();// optionaly:char * const p = str.c_str();
总结

以上是内存溢出为你收集整理的c – 将long转换为char * const全部内容,希望文章能够帮你解决c – 将long转换为char * const所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1219158.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存