如何在 Arduino 中将字符变量作为数字打印到串行端口? [英] How to print in Arduino a char variable as a number to the Serial Port?

查看:29
本文介绍了如何在 Arduino 中将字符变量作为数字打印到串行端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆