How to print in Arduino a char variable as a number to the Serial Port?

两盒软妹~` 提交于 2019-12-24 17:00:13

问题


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).

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.

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?

Thanks in advance.


回答1:


Cast the char variable to an unsigned char type:

Serial.print( (uint8_t) c );

This calls a different print method. They are "overloaded".




回答2:


store the variable as type byte instead.

byte val = 5;

then Serial.print(val) will give 5



来源:https://stackoverflow.com/questions/34133509/how-to-print-in-arduino-a-char-variable-as-a-number-to-the-serial-port

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!