Project 2.04 Section 2 ⚡ Embedded Relevance: Core char ASCII Escape Sequences Control Codes UART Framing

2.04 char Representations, ASCII Control Codes & Serial Protocol Framing

Executive Summary: Exploring character types and escape sequences. We examine ASCII encoding tables, character-to-integer conversion, and how non-printable ASCII control codes (STX 0x02, ETX 0x03, ACK 0x06, NAK 0x15, CR '\r', LF '\n') frame serial communication packets.

💻 1. Annotated Source Code

#include <iostream>
#include <string>
using namespace std;

int main() {
	char singleChar = 'a';
	string myName = "Rob";

	cout << singleChar << endl;
	cout << myName << endl;

	cout << "Hello " << myName << endl;

	return 0;
}

📐 2. Architecture & UML Class Model

📐 Character Encoding & ASCII Memory Mapping Model
+ Public - Private # Protected
<<compilation-unit>> TextManipulator ASCII Processor
-rawChar : char
-asciiCode : uint8_t
+toUpper(c: char) : char
+toLower(c: char) : char
+isDigit(c: char) : bool
+printHexCode(c: char) : void

📚 3. Core C++ Concepts Deep-Dive

1. Character Literals & ASCII Encodings

In C++, a char literal ('A') evaluates to its integer ASCII numeric representation (65 / 0x41).

2. Escape Sequences

Special characters are represented via escape sequences: '\n' (Line Feed - 0x0A), '\r' (Carriage Return - 0x0D), '\t' (Tab - 0x09), '\0' (Null terminator - 0x00).

⚡ 4. Embedded Systems & Hardware Reality

1. Serial Protocol Packet Framing

UART, RS-485, and Modbus ASCII protocols rely on control characters for message boundaries:

  • 0x02 (STX): Start of Text.
  • 0x03 (ETX): End of Text.
  • 0x0D 0x0A (CR LF): AT Command Line Terminations (GSM / GPS / WiFi modules).

💡 5. Production-Ready Embedded Refactoring

ASCII serial framing parser:

💡 Production-Ready Refactor
#include <cstdint>

enum class AsciiControl : uint8_t {
    STX = 0x02, // Start of Packet
    ETX = 0x03, // End of Packet
    ACK = 0x06, // Acknowledge
    NAK = 0x15, // Negative Acknowledge
    CR  = 0x0D, // Carriage Return
    LF  = 0x0A  // Line Feed
};

constexpr bool isPacketBoundary(uint8_t byte) noexcept {
    return byte == static_cast<uint8_t>(AsciiControl::ETX) || 
           byte == static_cast<uint8_t>(AsciiControl::LF);
}

📝 Knowledge Verification Quiz

Test your understanding of the C++ concepts and embedded microcontroller trade-offs covered in this guide. Click any option for instant feedback.

Q1. What is the ASCII integer value of the character '0' (zero)?
A 48 (0x30)
B 0
C 1
D 255
Detailed Explanation: ASCII character '0' is encoded as decimal 48 (hexadecimal 0x30). Subtracting '0' converts an ASCII digit character into its numeric integer value.
Q2. How does a null terminator '\0' differ from the digit character '0' in memory?
A '\0' has numeric value 0x00 (all bits zero), while '0' has numeric value 0x30 (decimal 48)
B They are identical in memory
C '\0' is 2 bytes while '0' is 1 byte
D '\0' cannot be stored in an array
Detailed Explanation: '\0' is the null byte with numerical value 0, whereas the ASCII character '0' is 0x30 (48).
Q3. Why is 'char' signedness implementation-defined in standard C++?
A The standard allows compilers to make 'char' signed or unsigned depending on CPU architecture efficiency (e.g. ARM defaults to unsigned char, x86 to signed char)
B To encrypt string variables
C Because char can hold 16 bits
D To prevent string manipulation
Detailed Explanation: Whether char is signed or unsigned is target-specific. On ARM Cortex-M, GCC defaults to unsigned char unless -fsigned-char is specified.
Q4. Which escape sequence represents the Carriage Return (0x0D) character commonly required in cellular modem AT commands?
A '\r'
B '\n'
C '\t'
D '\0'
Detailed Explanation: '\r' represents Carriage Return (ASCII 0x0D), used with Line Feed ('\n', 0x0A) to terminate AT commands.