Formatting ASCII Strings as Hexadecimal Literals for Systems Work
Converting ASCII to hex is essential when authoring BPF packet filters, SQL UNHEX() queries, embedded C lookup tables, or binary protocol test fixtures. Each ASCII byte expands cleanly to two hexadecimal digits, so a 20-byte ASCII header always yields exactly 40 hex characters.
Watch out for line-ending differences when pasting multi-line text: Unix systems use LF (0x0A) while HTTP/1.1, SMTP, and Windows text files use CRLF (0x0D 0x0A). Inspecting the exact hex output reveals hidden whitespace, trailing newlines, or curly smart quotes that break strict parsers.
- Output StylesSpace-separated (47 45 54), contiguous (474554), C array (0x47, 0x45), and \x47\x45 literals.
- ASCII ValidationFlags characters outside 0x00–0x7F and encodes them accurately as UTF-8 hex bytes.
- Zero PaddingAlways pads single-nibble control bytes (0x00–0x0F) to two hex digits (e.g., 0A for LF).
What is the hex code for a space, newline, and carriage return?
Space is 0x20, Line Feed (\n) is 0x0A, Carriage Return (\r) is 0x0D, and Horizontal Tab (\t) is 0x09.
How do I use the hex output inside a C or Rust byte slice?
Select the 0x comma-separated format (e.g., 0x47, 0x45, 0x54) and paste it directly inside uint8_t buf[] = { ... }; or let buf: &[u8] = &[...];.
What happens if my input contains non-ASCII Unicode characters?
Characters above U+007F are encoded into their multi-byte UTF-8 hex sequence and flagged so you know the buffer is no longer pure 7-bit ASCII.