C++将int转换为chars数组?

Edd*_*223 1 c++ arrays types casting char

可能重复:
C++将int和string转换为char*

你好,我正在制作游戏,我有一个记分板.得分存储在一个int变量中,但是我用于游戏的库需要一组字符,用于为我的记分板输出文本.

那么如何将int转换为字符数组呢?

int score = 1234;  // this stores the current score

dbText( 100,100, need_the_score_here_but_has_to_be_a_char_array); 
// this function takes in X, Y cords and the text to output via a char array
Run Code Online (Sandbox Code Playgroud)

我正在使用的库是DarkGDK.

tyvm :)

ybu*_*ill 8

ostringstream sout;
sout << score;
dbText(100,100, sout.str().c_str());
Run Code Online (Sandbox Code Playgroud)

  • 这也是我的建议.请记住.str()返回一个临时对象,因此缓存.str().c_str()的结果是一个坏的IDEA.(这不适用于您的示例,但我想确保提到了警告). (5认同)