如何在Arduino上将int转换为字符串?
如何将 int,n
转换为字符串,以便当我通过串行发送它时,它作为字符串发送?
这是我到目前为止所拥有的:
int ledPin=13;
int testerPin=8;
int n=1;
char buf[10];
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(testerPin, OUTPUT);
Serial.begin(115200);
}
void loop()
{
digitalWrite(ledPin, HIGH);
sprintf(buf, "Hello!%d", n);
Serial.println(buf);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
n++;
}
How do I convert an int, n
, to a string so that when I send it over the serial, it is sent as a string?
This is what I have so far:
int ledPin=13;
int testerPin=8;
int n=1;
char buf[10];
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(testerPin, OUTPUT);
Serial.begin(115200);
}
void loop()
{
digitalWrite(ledPin, HIGH);
sprintf(buf, "Hello!%d", n);
Serial.println(buf);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
n++;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
像这样使用:
您可以在此处找到更多示例。
Use like this:
You can find more examples here.
使用
stdlib.h
中包含的itoa()
函数use the
itoa()
function included instdlib.h
您可以简单地执行:
这会自动将
n
转换为 ASCII 字符串。请参阅Serial.println()
的文档。You can simply do:
which will convert
n
to an ASCII string automatically. See the documentation forSerial.println()
.你只需要将它包裹在一个 String 对象上,如下所示:
你也可以这样做:
You just need to wrap it around a String object like this:
You can also do:
这是将 int(有符号 16 位整数)转换为字符串的速度优化解决方案。
此实现避免使用除法,因为用于 Arduino 的 8 位 AVR 没有硬件 DIV 指令,编译器将除法翻译为耗时的重复减法。因此,最快的解决方案是使用条件分支来构建字符串。
从一开始就在 RAM 中准备一个固定的 7 字节缓冲区以避免动态分配。由于它只有 7 个字节,因此固定 RAM 使用的成本被认为是最低的。为了帮助编译器,我们在变量声明中添加寄存器修饰符以加速执行。
该草图使用与 Arduino v1.0.5 捆绑在一起的 avr-gcc 编译为 1,082 字节的代码(int2str 函数本身的大小为 594 字节)。与使用编译为 2,398 字节的 String 对象的解决方案相比,此实现可以减少您的代码大小 1.2 Kb(假设您不需要其他 String 的对象方法,并且您的数字严格为有符号 int 类型)。
通过用适当的汇编代码编写该函数可以进一步优化。
This is speed-optimized solution for converting int (signed 16-bit integer) into string.
This implementation avoids using division since 8-bit AVR used for Arduino has no hardware DIV instruction, the compiler translate division into time-consuming repetitive subtractions. Thus the fastest solution is using conditional branches to build the string.
A fixed 7 bytes buffer prepared from beginning in RAM to avoid dynamic allocation. Since it's only 7 bytes, the cost of fixed RAM usage is considered minimum. To assist compiler, we add register modifier into variable declaration to speed-up execution.
This sketch is compiled to 1,082 bytes of code using avr-gcc which bundled with Arduino v1.0.5 (size of int2str function itself is 594 bytes). Compared with solution using String object which compiled into 2,398 bytes, this implementation can reduce your code size by 1.2 Kb (assumed that you need no other String's object method, and your number is strict to signed int type).
This function can be optimized further by writing it in proper assembler code.
这对我来说很简单:
This simply work for me:
在 Arduino 中,使用
String
关键字创建String
类的对象,该对象具有多个版本的构造函数。如果在实例化时将整数作为参数传递,则它包含数字的 ASCII 表示形式。请查看 arduino 字符串参考。
In Arduino, using the
String
keyword creates an object of theString
class which has multiple versions of its constructor. If an integer is passed as an argument while instantiating, it contains the ASCII representation of the numbers.Please check out the arduino String reference.
解决方案太大了。试试这个简单的。请提供 7 个以上字符的缓冲区,未进行检查。
如果您丢弃索引“l”并直接增加缓冲区,则可以轻松修改以提供缓冲区的后端。
The solution is much too big. Try this simple one. Please provide a 7+ character buffer, no check made.
Can be easily modified to give back end of buffer, if you discard index 'l' and increment the buffer directly.
下面是一个自编写的 myitoa() ,它的代码要小得多,并且在 char *mystring 中保留一个 7 的固定数组(包括终止 0),这通常是可取的。显然,如果需要可变长度的输出字符串,则可以使用字符移位来构建代码。
Here below is a self composed myitoa() which is by far smaller in code, and reserves a FIXED array of 7 (including terminating 0) in char *mystring, which is often desirable. It is obvious that one can build the code with character-shift instead, if one need a variable-length output-string.
欲了解更多信息,您可以访问 arduino 网站
https://www.arduino.cc/en/Serial/Println
希望这会帮助你。
谢谢!
for more you can visit to the site of arduino
https://www.arduino.cc/en/Serial/Println
wish this will help you.
thanks!