DATA REPRESENTATION CHECKS

Checking a byte value against the text spelling of its hex notation

Hex digits describe byte values, and the characters used to write those digits are text with bytes of their own. This worksheet decodes the pair 41 and then encodes the same two characters to show the difference.

Two meanings for the same four keystrokes

The characters 4 and 1, typed side by side, can mean two quite different things. Read as hexadecimal byte notation, 41 is a single byte whose value is 4 times 16 plus 1, which is 65, and in ASCII and UTF-8 that value is the capital letter A. Read as plain text, 41 is two characters, the digit four and the digit one, and each of them has its own byte. The UTF-8 byte converter has two modes that make the choice explicit. Decode mode treats the input as hexadecimal byte notation and returns the text it represents, while encode mode treats the input as text and returns the bytes of that text in hexadecimal. Running both on 41 shows the two meanings side by side. The same holds for any pair of hex digits. The pair 4A is the letter J when decoded, and as text it is the characters 4 and A, which encode to 34 41.

Decoding the pair 41

In decode mode, the input 41 is split into one byte, 0x41. The converter applies strict UTF-8 decoding to that byte. Values below 128 in UTF-8 are identical to ASCII, and 65 is the code for A, so the output is A. The arithmetic behind the pair is simple. Each hexadecimal digit stands for four bits, so two digits make eight bits, which is one byte. The first digit counts sixteens and the second counts ones. For 41, that is 4 sixteens and 1 one. Hex is popular for bytes because of this tidy fit: the entire range from 0 to 255 needs exactly two digits, from 00 to FF.

Encoding the text 41

In encode mode, the same input is treated as the two-character string 4 followed by 1. The digit character 4 has the code point U+0034, which is 52 in decimal and 34 in hexadecimal. The digit character 1 has the code point U+0031, which is 49 in decimal and 31 in hexadecimal. Both are ASCII, so each takes a single byte in UTF-8, and the converter returns 34 31. The result has two bytes where the decoded interpretation had one. The digits of the output, 34 and 31, are hex notation again, this time describing the bytes of the text 41. Seeing notation produce notation can be confusing, so say clearly which layer you are looking at.

Layers of representation

There are always layers: the value, its notation, and the characters used to write the notation. The byte value 65 is a quantity. It can be written in decimal as 65, in binary as 01000001, or in hexadecimal as 41. Those are three notations for one value. When the notation is itself stored in a text file, it becomes a string of characters, each of which is encoded into bytes by a text encoding such as UTF-8. A hex dump in a log file is therefore several bytes of text per byte of data. Confusion arises when two layers are treated as one, for example when a field expecting raw bytes receives the hex spelling of those bytes.

Size consequences

Storing bytes as hex text doubles their size. A SHA-256 digest is 32 bytes long, and its usual hexadecimal spelling has 64 characters, which take 64 bytes in UTF-8. An MD5 digest is 16 bytes and has a 32 character hex spelling. A database column sized for 32 bytes can hold the raw digest but not the hex text. The reverse mistake is also possible: passing the 64 character string to a function expecting 32 raw bytes triggers a length error or, worse, silently hashes the wrong thing. Whenever a size is stated, ask whether it counts bytes of data or characters of notation. The same doubling applies to any raw byte data written as hex text, so a 1 megabyte file takes about 2 megabytes when stored as its hex spelling. Likewise, a 20 byte payload shown as 40 hex characters is a common sign of a SHA-1 digest, whose 160 bits make exactly 20 bytes.

Common spellings of byte notation

Byte values appear in many conventions. Source code often writes 0x41, with a prefix, or uses an escape such as backslash x 41 inside a string. URLs use percent encoding, where %41 means the byte 0x41. Dumps show bytes separated by spaces, such as 48 65 6C 6C 6F, which spell Hello. Base16, defined in RFC 4648, is the formal name for hexadecimal encoding of bytes. Upper and lower case letters are both used for the digits A to F, so 4A and 4a mean the same byte. The converter works with plain hexadecimal pairs separated by spaces, and it rejects input such as 0x41, so remove prefixes and punctuation that belong to other conventions before decoding.

A second encode example

The text Hi has two characters. H is U+0048, which is byte 48, and i is U+0069, which is byte 69. Encode mode returns 48 69, and the bytes in decimal are 72 and 105. If a message instead contains the characters 4, 8, 6 and 9 as text, encode mode gives 34 38 36 39, four bytes for four characters. That text spells the hex notation of Hi, which is another layer entirely. The two strings 48 69 and 34 38 36 39 can look similar on a screen, yet they have different lengths and different meanings, which is why a field label stating bytes or text is worth its space.

Characters outside ASCII

UTF-8, defined in RFC 3629, uses one to four bytes per character. Characters below U+0080 use one byte. The letter e with an acute accent, U+00E9, uses two bytes, C3 A9. The euro sign, U+20AC, uses three, E2 82 AC. Most emoji use four. Hex notation for such characters therefore has more than one pair. Decoding requires the full set of bytes: C3 alone is not valid UTF-8, and a strict decoder rejects it instead of guessing. The pair 41 in this worksheet is the simplest possible case, but the same principle holds for longer sequences, and a mistake in grouping the pairs changes the decoded text. The leading byte E2 of the euro sign signals that two continuation bytes follow.

Odd lengths and invalid pairs

Hex notation for bytes must have an even number of digits, because each byte needs two. A string like 414 cannot be split into whole bytes, and it is ambiguous whether a leading zero was dropped; the converter answers with an error asking for pairs separated by spaces. Characters outside 0 to 9 and A to F are not hex digits at all. A strict tool rejects such input and reports the problem. Quietly padding or skipping characters can produce plausible but wrong bytes. When copying from a document, check for hidden characters such as non-breaking spaces or line breaks, which look like separators and are not. Even a valid run of hex digits is not always valid text. The byte FF never appears in well-formed UTF-8, so decoding the pair FF fails. A string such as 4G contains a character outside the hex digits, so the pair fails before any byte is formed.

Choosing the right mode

Before using either mode, decide what your input is. If the text you hold came from a hex dump, a protocol specification or a debugger memory view, it is notation and decode mode applies. If it is a message, a name or a password-like string whose bytes you want to inspect, it is ordinary text and encode mode applies. A quick test is to ask whether the string could contain letters outside A to F. A name like Alice cannot be hex notation, since l and i are not hex digits. A string like 41 is ambiguous, which is exactly why the page uses it. The tool does not guess, and the choice has to come from knowing where the data came from. If the source is a dump that mixes offsets, spaces and a text column, copy only the hex column before decoding.

A byte seen in several notations

The byte at the center of this worksheet can be written in several ways. In decimal it is 65. In binary it is 01000001, in hexadecimal 41, and as a character in ASCII or UTF-8 it is A. The lowercase letter a is 61 in hexadecimal, which is 32 higher, so the case of a Latin letter is one bit of its code. The digit characters occupy a block starting at hexadecimal 30, with 0 at 30, 1 at 31 and 4 at 34. That explains the output of encode mode for the text 41: the character 4 is 30 plus 4, and the character 1 is 30 plus 1, giving 34 and 31. Knowing the block layout lets you read many hex dumps by eye, since a run of bytes between 30 and 39 is usually a string of decimal digits.

Double encoding and how it shows up

A common bug is applying hex encoding twice. A program turns the byte 41 into the text 41, which is already one layer too many for a field that wanted the byte, and a second pass turns that text into the bytes 34 31 and then into the text 3431. The symptom is a value that is longer than expected and consists only of the digits 0 to 9 and the letters A to F. Another symptom is a decoded result that looks like more hex, such as getting 41 back after one decode when the original text was A. When this happens, decode one layer at a time and examine the output at each stage. The worksheet pair of results, A from decoding and 34 31 from encoding, are the two single steps from which longer chains are built.

Reproduce the checks

Decode byte notation

Open the Utf8 Byte Converter and enter:

Text or encoded input
41
Calculation mode
decode

Expected output:

Hex

A

The pair is interpreted as one byte.

Encode the notation as ordinary text

Open the Utf8 Byte Converter and enter:

Text or encoded input
41
Calculation mode
encode

Expected output:

Hex

34 31

The two written digits are encoded as characters.

References and implementation scope

The examples describe the behavior of the linked Computandum tools. A successful local check does not establish that another application accepts the same format or applies the same limits.

Sources

Related tasks