将long转换为char * const [英] Convert long to char* const

查看:177
本文介绍了将long转换为char * const的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中将 long 转换为 char * const 的正确方法是什么?



编辑:

  long l = pthread_self 
ThirdPartyFunction(Thread_Id _+ l); //需要这样做


ThirdPartyFunction(char * const identifierString)
{}


解决方案

编辑:
在C ++中将整数转换为字符串的正确方法是使用stringstream。例如:

  #include< sstream> 

std :: ostringstream oss;
oss<< Thread_Id_<< l;
ThirdPartyFunction(oss.str()。c_str());

现在,这可能不是最快的方式

>

取决于convert的意思。



要转换 long 的内容到指针:

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

要查看 long 数组 char s:

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

要将 long 转换为 string

  std :: ostringstream oss; 
oss<<你的_
std :: string str = oss.str();
// optionaly:
char * const p = str.c_str();


What is the right way to convert long to char* const in C++?

EDIT:

long l = pthread_self();
ThirdPartyFunction("Thread_Id_"+l); //Need to do this


ThirdPartyFunction(char* const identifierString)
{}

解决方案

EDIT: The "proper" way to convert an integer to a string, in C++, is to use a stringstream. For instance:

#include <sstream>

std::ostringstream oss;
oss << "Thread_Id_" << l;
ThirdPartyFunction(oss.str().c_str());

Now, that probably won't be the "fastest" way (streams have some overhead), but it's simple, readable, and more importantly, safe.


OLD ANSWER BELOW

Depends on what you mean by "convert".

To convert the long's contents to a pointer:

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

To "see" the long as an array of chars:

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

To convert the long to a string:

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

这篇关于将long转换为char * const的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆