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
<<compilation-unit>>
TextManipulator
ASCII Processor
Attributes / Data Members
-rawChar : char
-asciiCode : uint8_t
Operations / Methods
+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)?
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?
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++?
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?
Detailed Explanation:
'\r' represents Carriage Return (ASCII 0x0D), used with Line Feed ('\n', 0x0A) to terminate AT commands.