如何从int转换为char *? [英] how to convert from int to char*?

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

问题描述

我知道的唯一方法是:

#include <sstream>
#include <string.h>
using namespace std;

int main() {
  int number=33;
  stringstream strs;
  strs << number;
  string temp_str = strs.str();
  char* char_type = (char*) temp_str.c_str();
}

但是有没有更少打字的方法?

But is there any method with less typing ?

推荐答案


  • 在C ++ 11中,使用 std :: to_string as:

    std::string s = std::to_string(number);
    char const *pchar = s.c_str();  //use char const* as target type
    


  • 're done is just fine,except use const as:

    char const* pchar = temp_str.c_str(); //dont use cast
    


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

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