加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 将long转换为char * const

发布时间:2020-12-16 10:18:38 所属栏目:百科 来源:网络整理
导读:在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 this


ThirdPartyFunction(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();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读