在 C++ 中将整数存储到 char* 中

发布于 2024-08-15 16:46:59 字数 138 浏览 4 评论 0原文

我正在编写一些返回整数的代码,然后需要使用 ncurses 库中的 printw 输出该整数。但是,由于 printw 只接受 char*,我不知道如何输出它。

本质上,有没有办法将整数存储到 char 数组中,或者使用 printw 输出整数?

I'm writing some code that returns an integer, which then needs to be outputted using printw from the ncurses library. However, since printw only takes char*, I can't figure out how to output it.

Essentially, is there a way to store a integer into a char array, or output an integer using printw?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萌辣 2024-08-22 16:46:59

printw() 接受 const char * 作为格式说明符。你想要的是

printw("%d",yournumber);

printw() accepts const char * as a format specifier. What you want is

printw("%d",yournumber);
岁月苍老的讽刺 2024-08-22 16:46:59

itoa 函数将 int 转换为 char*。

The itoa function converts an int to char*.

似最初 2024-08-22 16:46:59

使用 itoa() 或 sprintf() 将整数转换为 ascii 字符串。

示例:

char s[50];
sprintf(s, "%d", someInteger);

现在你可以将 s 作为 char* 传递

Use itoa() or sprintf() to convert integer to ascii string.

Example:

char s[50];
sprintf(s, "%d", someInteger);

now u can pass s as char*

东北女汉子 2024-08-22 16:46:59

itoa 将为您提供帮助。

itoa will help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文