4

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.

roschach
  • 8,390
  • 14
  • 74
  • 124

3 Answers3

4

Cast the char variable to an unsigned char type:

Serial.print( (uint8_t) c );

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

roschach
  • 8,390
  • 14
  • 74
  • 124
slash-dev
  • 1,569
  • 2
  • 9
  • 9
0

store the variable as type byte instead.

byte val = 5;

then Serial.print(val) will give 5

Zoe
  • 27,060
  • 21
  • 118
  • 148
0

Other two answers that suggest using byte or uint8_t(they are equal) , but those two data type are both unsigned,ranging from 0 to 255. If you want to print negative numbers, you can try this way:

char x=-1;
Serial.print(x,DEC);