Lesson 61: Using char() Conversion
Learn how char() conversion maps numeric values into single-character codes and when it is useful for Serial text and protocol bytes.
Progress indicator
Lesson 61 of 61
Learning Objectives
- Understand what char() conversion does in Arduino.
- Learn how numbers map to characters using ASCII-style codes.
- Differentiate char() conversion from declaring a char variable.
- Use char() safely when building Serial text or protocol bytes.
- Avoid mistakes when converting out-of-range or unintended values.
- Handle printable vs non-printable char codes correctly in Serial output.
Concept Explanation
What is char() Conversion
char() converts a numeric value into char type.
In practice, the resulting character is interpreted by its code value (commonly ASCII for printable text).
char() Syntax
char c = char(value);
char startByte = char(2);How char() Works
- Evaluate source number/expression.
- Take low 8-bit character value.
- Store value in char variable.
- When printed, display mapped character if printable.
If code is non-printable (for example control characters), Serial output may look blank or behave unexpectedly. In those cases, also print numeric code for debugging clarity.
Type Casting Concept
Casting means explicitly forcing one type into another.
int code = 65;
char letter = char(code); // 'A'char() vs char Variable
char x;declares a variable type.char(expr)converts an expression result to char.- You can combine both:
char x = char(value);
Quick rule: left side defines storage type, right side controls conversion behavior.
When to Use char()
- Converting numeric codes to readable characters in Serial Monitor.
- Building lightweight text/packet protocol markers.
- Generating symbol/digit/letter output from computed values.
- Converting keypad/button numeric codes into character commands.
Example Code
This sketch converts numeric codes to characters and prints them in Serial output.
const int LED_PIN = 2;
int asciiA = 65;
int asciiZ = 90;
int digitCode = 51;
int symbolCode = 35;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
char letterA = char(asciiA);
char letterZ = char(asciiZ);
char digit = char(digitCode);
char symbol = char(symbolCode);
if (letterA == 'A') {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
Serial.print("char(65)=");
Serial.print(letterA);
Serial.print(", char(90)=");
Serial.print(letterZ);
Serial.print(", char(51)=");
Serial.print(digit);
Serial.print(", char(35)=");
Serial.println(symbol);
delay(800);
}Example Code Explanation
char(65)becomes'A'.char(90)becomes'Z'.char(51)becomes'3'(digit character).char(35)becomes'#'symbol.- LED turns ON only if converted value matches
'A'. - Serial prints converted characters so mapping is visible clearly.
- This is useful when incoming numeric code should be interpreted as command letters or symbols.
What Happens Inside
- Numeric value is evaluated first.
- Conversion turns it into char code value.
- Program uses char in comparison and output.
- Serial displays printable character if code maps to printable range.
- For non-printable codes, numeric debug prints are safer than direct char prints.
| Input | char(input) | Meaning |
|---|---|---|
| 65 | A | Uppercase letter A |
| 51 | 3 | Digit character 3 |
| 35 | # | Symbol hash |
| 10 | (control) | Line-feed/newline control code |
Common Mistakes with char()
- Assuming numeric value prints as number after converting to char.
- Using non-printable control codes and expecting readable text.
- Confusing
'3'character with numeric value3. - Comparing char codes incorrectly without quotes (e.g.,
Ainstead of'A'). - Assuming
char(3)means digit'3'; it is actually control code 3.
Best Practices for char()
- Use clear comments when converting protocol byte values to characters.
- Keep printable range checks if output should remain human-readable.
- Use single quotes for char literals:
'A','#','0'. - Verify conversion behavior with Serial logs while building logic.
- When unsure, print both char and integer code to avoid interpretation mistakes.
Try it now
Open the simulator workspace and inspect char() conversion outputs step-by-step.