将 int 附加到 char* 然后清除
我正在使用 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 int
s). 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
char 是单个字符,而 char* 可以是指向字符的指针或指向 C 字符串中第一个字符的指针,C 字符串是一个以空字符结尾的字符数组。
您不能使用 char 来表示长度超过 1 位的整数,因此我假设您实际上是指 char* 。
如果您有,
那么您可以使用 sprintf 将
buffer
设置为表示 intn
的字符串,当您完成后,您可以使用
希望 清除该字符串我们要求的,祝你好运!
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
then you can set
buffer
to a string representing an intn
with sprintfAnd when you're done with it, you can clear the string with
Hope that's what you were asking for, and good luck!
您无法读取 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.