如何在 Arduino 中将字符变量作为数字打印到串行端口? [英] How to print in Arduino a char variable as a number to the Serial Port?
问题描述
我正在使用 Arduino Uno.我正在从 EEPROM 读取一个字节值并将该值存储在 char
类型的变量中(1 个字节).
I am using Arduino Uno. I am reading a byte value from the EEPROM and storing the value in a variable of type char
(1 byte).
我想将变量的值作为数字(而不是相应的 ASCII 代码)打印到串行监视器.例如考虑 char val = 5
.我想看到串行监视器 5
而不是 ASCII 值.
I want to print the value of the variable as a number (not to the corresponding ASCII code) to the Serial Monitor. For example consider char val = 5
.
I want to see to the Serial Monitor 5
and not the ASCII value.
我尝试了 Serial.print(val)
和 Serial.write(val)
但结果是一样的:它总是打印 ASCII 代码.
I tried both Serial.print(val)
and Serial.write(val)
but the result is the same: it prints always the ASCII code.
如何将值打印到串行监视器?
How can I print the value to the Serial Monitor?
提前致谢.
推荐答案
将 char
变量转换为 unsigned char
类型:
Cast the char
variable to an unsigned char
type:
Serial.print( (uint8_t) c );
这会调用不同的 print
方法.他们超载"了.
This calls a different print
method. They are "overloaded".
这篇关于如何在 Arduino 中将字符变量作为数字打印到串行端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!