Hex Encoder & Decoder

Hex writes each byte as two digits, 00 through FF. Text is UTF-8 first, so an ASCII letter is two digits and a CJK character is usually six. A and a are the same value. A leading 0x is integer notation, not a byte, and spaces only group pairs. The digit count has to be even. Bytes that are not UTF-8 will not come back as text. Base64, binary, and a \u code-point escape are different spellings of related but not identical data.

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

  1. 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.
  2. 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.
  3. 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

  1. 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.
  2. A and a are the same nibble. Case never changes the byte. Dumps mix case only for reading.
  3. 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

  1. Two nibbles make one byte. One leftover digit has nowhere to go, so the length after separators are removed must be even.
  2. Only 0-9 and A-F are hex digits. G, a comma, or a colon left in the string is not a nibble.
  3. 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

  1. 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.
  2. 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.
  3. 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

  1. 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.
  2. Binary writes the same eight bits as 0 and 1. Hex groups those bits into nibbles. The byte is identical. Only the spelling changes.
  3. 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.

Common questions

What is hexadecimal encoding?
It writes each byte as two hexadecimal digits. A byte is eight bits, and each hex digit carries four of them, so the range of one byte is 00 through FF. For text, the bytes are the UTF-8 encoding of the characters, not the number of letters on screen. The mapping is public and reversible when the bytes are kept intact. It does not hide the data.
Why does a short Chinese word become a long hex string?
A typical CJK ideograph is three UTF-8 bytes. Each byte is two hex digits, so one character is six hex digits. Four characters are twenty-four hex digits before any spaces. The string looks long because hex expands every byte, and those characters are several bytes each. The character count was never the length of the hex.
Do uppercase and lowercase hex digits mean different bytes?
No. A and a are the same nibble, and 0A and 0a are the same byte. Dumps change case to make a header or a checksum easier to scan. A decoder that accepts A-F must accept a-f as the same values. Only a character outside 0-9 and A-F, such as G, is not a hex digit.
Should a 0x prefix be removed before decoding hex?
A 0x prefix is how programming languages mark an integer written in hex. It is not the first byte. A byte string copied from a debugger sometimes includes one 0x at the start and no 0x on the later bytes. That single prefix is not data. A 0x between bytes, or a 0x on every byte, is a different spelling and is not the same as one leading prefix.
Why does hex decode fail when the number of digits is odd?
Each byte is exactly two hex digits. After spaces are removed, an odd count leaves one nibble without a partner, so it is not a sequence of bytes. The usual cause is a truncated copy that dropped the last digit. Adding a zero to force an even length invents a different final byte. The missing digit has to be recovered from the original dump.
Why is the hex valid but the decoded text garbage or an error?
Valid hex only means each pair is a byte. Those bytes are text only if they are UTF-8. Illegal UTF-8 is rejected. A file header, compressed data, or UTF-16 text is well-formed hex and still not UTF-8. Decoding it as text produces an error or unrelated characters. The hex then needs the decoder for that file format, not a text charset.
What is the difference between hex and Base64?
Both represent the same bytes. Hex uses sixteen symbols and two characters per byte, so the result is longer and easy to read one byte at a time. Base64 uses sixty-four symbols and four characters for every three bytes, so it is shorter and harder to inspect by eye. Hashes and protocol fields are often left in hex. Payloads that must sit inside JSON or mail are often left in Base64.
How is a hex byte dump different from a \u escape?
A hex dump lists UTF-8 bytes. A \u escape lists a code point, usually four hex digits for one UTF-16 code unit. The ideograph at U+4E2D is not the byte sequence E4 B8 AD. Using the code-point digits as if they were UTF-8 bytes, or the reverse, yields a different string. Code points belong with Unicode escape. The eight bits of each byte, written as 0 and 1, are the same bytes again and belong with binary encode and decode.