将 int 附加到 char* 然后清除

发布于 2024-07-23 11:50:02 字数 319 浏览 7 评论 0原文

我正在使用 Arduino 开发一个项目,因此,我正在从串行端口读取数据(发送 int)。 然后我需要将此串行通信写入 LCD,它需要一个 char*

我需要从串口读取几个字符(两个整数)到一个字符串中。 收到这两个字符后,我需要清除字符串以准备接下来的两个字符。

TLDR:如何将 int 附加到 char*,然后在字符串有两个字符后清除它?

I'm working on a project using an Arduino and as such, I'm reading from a serial port (which sends ints). I need to then write this serial communication to an LCD, which takes a char*.

I need to read several characters from the serial port (two integers) into a string. After both have been received, I then need to clear the string to prepare for the next two characters.

TLDR: How do I append an int to a char*, and then clear the string after it has two characters?

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

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

发布评论

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

评论(2

饮惑 2024-07-30 11:50:02

char 是单个字符,而 char* 可以是指向字符的指针或指向 C 字符串中第一个字符的指针,C 字符串是一个以空字符结尾的字符数组。

您不能使用 char 来表示长度超过 1 位的整数,因此我假设您实际上是指 char* 。

如果您有,

char buffer[10];

那么您可以使用 sprintf 将 buffer 设置为表示 i​​nt n 的字符串

sprintf(buffer, "%d", n);

,当您完成后,您可以使用

sprintf(buffer, "");

希望 清除该字符串我们要求的,祝你好运!

A char is a single character, whereas a char* can be a pointer to a character or a pointer to the first character in a C string, which is an array of chars terminated by a null character.

You can't use a char to represent an integer longer than 1 digit, so I'm going to assume you did in fact mean char*.

If you have

char buffer[10];

then you can set buffer to a string representing an int n with sprintf

sprintf(buffer, "%d", n);

And when you're done with it, you can clear the string with

sprintf(buffer, "");

Hope that's what you were asking for, and good luck!

小镇女孩 2024-07-30 11:50:02

您无法读取 char *,它是一个指针。 您可以读入指针所指向的内存,只要它指向有效的内容。 至于清理,你的意思并不明显。

最重要的是,您需要发布一些尝试执行您想要的操作的实际代码,并询问这一点。

You can't read into a char *, it is a pointer. You can read into the memory pointed to by the pointer, provided it points to something valid. As for clearing, it's not obvious what you mean by that.

Bottom line is that you need to post some actual code that attempts to do what you want, and ask about that.

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