What hexadecimal notation says about a byte
Hexadecimal writes each byte as two digits from 0-9 and A-F. One hex digit is four bits, a nibble, so two digits are the eight bits of one byte, from 00 to FF. Text is turned into UTF-8 bytes first. An ASCII letter is one byte and two hex digits. A character outside ASCII is several bytes and a longer run of digits. Upper and lower case name the same value: 0A and 0a are the same byte.
A leading 0x is integer notation, not a byte. Spaces between pairs are only grouping. The digit count has to be even, because a leftover nibble is not a whole byte. Decoding those bytes as text succeeds only when they are valid UTF-8. A gzip header, an image, or a UTF-16 dump will not come back as the characters someone hoped for.
Hex is the expanded form of a byte. Base64 packs the same bytes into a 64-character alphabet. Binary encode and decode shows the eight bits instead of two hex digits. A \u escape names a code point, not the UTF-8 bytes of that character.
How nibbles, UTF-8, and other byte spellings line up
Why one character can become two, four, or six hex digits
- The encoder counts UTF-8 bytes, not letters. Code points U+0000 through U+007F are one byte, so they are exactly two hex digits.
- U+0080 through U+07FF is two bytes and four hex digits. Most CJK ideographs are three UTF-8 bytes and six hex digits. Emoji often take four bytes and eight hex digits.
- The character count of the string is not the hex length. A short word in Chinese is a long hex string because each character contributed three bytes.
What a pair of hex digits, a space, and a 0x prefix each mean
- Each digit is a nibble. The first digit of a pair is the high four bits, the second is the low four bits. 4A is the byte whose value is 74 in decimal.
- A and a are the same nibble. Case never changes the byte. Dumps mix case only for reading.
- Spaces, tabs, and newlines between digits are separators. A 0x or 0X glued to the front belongs to programming-language integer syntax and is not part of the byte string. A colon or a dash between bytes is a different convention and is not whitespace.
Why an odd number of digits or a non-hex character cannot be a byte string
- Two nibbles make one byte. One leftover digit has nowhere to go, so the length after separators are removed must be even.
- Only 0-9 and A-F are hex digits. G, a comma, or a colon left in the string is not a nibble.
- A truncated dump that lost the last digit of a pair is incomplete. Padding it with a zero changes the bytes. The missing digit has to come from the original dump.
When valid hex still does not decode to readable text
- Hex only records bytes. Turning those bytes into characters requires a character encoding. UTF-8 is the encoding used when the source was Unicode text.
- A byte sequence that is illegal UTF-8 cannot be decoded as text even though every digit is a valid hex digit. Fatal UTF-8 decoding rejects it.
- PNG, gzip, and UTF-16LE files are valid bytes and valid hex, and they are not UTF-8 text. The hex is a dump of the file, not a spelling of the words inside it.
How hex differs from Base64, binary, and a Unicode code-point escape
- Hex uses 16 symbols and two characters per byte. Base64 uses 64 symbols and four characters per three bytes, so the same payload is shorter and uses a different alphabet.
- Binary writes the same eight bits as 0 and 1. Hex groups those bits into nibbles. The byte is identical. Only the spelling changes.
- A \u escape followed by four hex digits names a UTF-16 code unit, usually one Unicode code point. The UTF-8 bytes of that character are a different sequence. U+4E2D is not the three bytes E4 B8 AD written without spaces.
Where a hex dump is bytes and not the text people expect
A protocol log that prints each byte as two digits
Packet and serial logs often separate bytes with spaces so a reader can see the boundary. The spaces are not data. The order is the order of the bytes on the wire, high nibble first inside each byte. Interpreting a multi-byte integer still needs the endianness of that field. The hex dump itself does not say whether a 16-bit field is big-endian or little-endian.
A hash or a key shown in hex next to a text encoding
SHA and similar digests are raw bytes printed in hex so every byte is visible. They are not UTF-8 text, and decoding them as text will fail or look like noise. Base64 is the other common spelling of those same digest bytes. Neither spelling is the original message.
A character someone looked up as a code point and then as UTF-8
Tables list U+4E2D for one ideograph. The UTF-8 encoding of that code point is the three bytes E4 B8 AD. A \u escape refers to the code point. Mixing the code-point hex with the UTF-8 hex produces a different character or an illegal sequence. Code-point escapes are the subject of Unicode escape.