Bits, Bytes, and Hexadecimal
Binary and hexadecimal are ways to write values. Learning to move between them makes Bitcoin's byte sequences easier to read without changing the underlying data.
Contents
Bits and bytes
A bit has two possible values: 0 and 1. In binary notation, each position represents a power of two. Reading from right to left, the positions have values 1, 2, 4, 8, and so on. Add the values of the positions containing a one to obtain the unsigned number.
A byte contains eight bits. That gives 2⁸ = 256 possible patterns, from 00000000 to 11111111. Interpreted as an unsigned integer, these patterns represent 0 through 255. A byte can also represent text or part of another value; the pattern alone does not determine its meaning.
Reading hexadecimal
Hexadecimal, usually shortened to hex, uses sixteen digits: 0 through 9, followed by a, b, c, d, e, and f for values ten through fifteen. Uppercase letters describe the same values.
Four bits form a nibble. Four bits have sixteen possible patterns, so one hex digit represents exactly one nibble. Two hex digits represent one byte. To convert binary to hex, group bits into fours: 0100 0001 becomes 4 1, written as 41. The grouping spaces help us read; they are not extra bytes.
One byte, several views
- Hexadecimal
41- Binary
01000001- Unsigned integer
65- ASCII / UTF-8 text
A
In binary, the set positions contribute 64 + 1 = 65. In hex, 4 × 16 + 1 = 65. ASCII and UTF-8 assign this byte to the letter A.
These are views of one byte, rather than four different pieces of data. Text decoding is one possible interpretation, not evidence that the byte was intended as a letter.
Lengths and leading zeros
When writing a byte sequence as hex, keep two digits for each byte, including leading zeros. The value zero occupies a byte when it appears as 00. Removing it changes the sequence and its length.
| Hex bytes | Byte length | Big-endian integer |
|---|---|---|
00 ff | 2 bytes | 255 |
ff | 1 byte | 255 |
The unsigned values match in big-endian order. The two-byte sequence still includes its leading zero byte.
Count two hex digits per byte: a 32-byte sequence takes 64 hex characters when written without a prefix or separators. This tells us its length, but does not identify what the sequence means.
Prefixes and binary display
The prefixes 0x and 0b identify hexadecimal and binary notation. For example, 0x41 and 0b01000001 both describe the unsigned value 65. The prefixes are notation, not additional stored bytes when the input is parsed as a number or hex data.
The interpreter's binary output preserves eight bits per byte. The bytes 00 ff display as 00000000 11111111 when grouped for reading. Leading zero bits remain visible, so the display reflects the two-byte sequence rather than only its numeric value.
Hexadecimal data and text
The text 41 contains two characters. Encoding those characters as UTF-8 produces the bytes 34 31. Decoding 41 as hexadecimal instead produces one byte, 41. Specify the intended format when the characters could mean either.
In the Data Interpreter, Auto reads a string consisting of hex digits as hexadecimal. Entering 16 therefore produces the byte 16, whose unsigned decimal value is 22. Selecting Text instead encodes the characters as 31 36. Auto does not turn a hex-looking string into a decimal integer.
Try the interpreter
Open the Data Interpreter to try these examples.
- Select Hex and enter
41. Compare binary, the unsigned number, and text. - Compare
00ffwithffin Hex format. Check byte length as well as the big-endian value. - Enter
41in Text format, then in Hex format. Compare34 31with41.
Related Library topics
- Data Types explains why the same bytes can have different meanings.
- Endianness explains numeric interpretation across several bytes.
- CompactSize introduces an encoding for counts and lengths.