const-char

how to convert from int to char*?

风流意气都作罢 提交于 2019-11-27 06:09:42
The only way I know is: #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 ? Nawaz In C++17, use std::to_chars as: std::array<char, 10> str; std::to_chars(str.data(), str.data() + str.size(), 42); In C++11, use std::to_string as: std::string s = std::to_string(number); char const *pchar = s.c_str(); //use char const* as target type And in C++03, what you're doing is just fine, except use const as: char const

how to convert from int to char*?

走远了吗. 提交于 2019-11-26 11:52:32
问题 The only way I know is: #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 ? 回答1: In C++17, use std::to_chars as: std::array<char, 10> str; std::to_chars(str.data(), str.data() + str.size(), 42); In C++11, use std::to_string as: std::string s = std::to_string(number); char const *pchar = s.c_str(); //use