Everything Is a Run of Bytes
Every stored thing is a sequence of bytes plus an agreement about how to read them. The bytes are the fact and the agreement is the convention, and only one of them is in the file.
Bytes and an agreement
A disk stores numbers. Each one is between zero and two hundred and fifty five, and the usual name for one of them is a byte. A file is a run of those numbers with a length. That is the whole of what is stored.
Everything else, that this file is a photograph and that one is a spreadsheet, that these eight bytes are a price and those four are a date, is an agreement held somewhere other than in the bytes. The agreement is real and necessary and it is not in the file.
| step | the agreement | what the five bytes 72 101 108 108 111 become | is it valid |
|---|---|---|---|
| 1 | letters in the common Western scheme | the word hello | yes |
| 2 | five separate small whole numbers | 72, 101, 108, 108, 111 | yes |
| 3 | four of them as one whole number, then one spare | 1214606444, and 111 left over | yes |
| 4 | pixel brightnesses in a row | five grey dots, mid to light | yes |
| 5 | a musical instrument instruction | a note on, of middling pitch | yes |
Where the agreement actually lives is worth listing, because each place it can live is a place it can be lost. In the name of the file, as an extension, which anybody can change. In the first few bytes of the file, as a marker that particular formats agree to begin with. In a column definition in a database. In a header sent alongside the file over a network. And, most often, in the assumption of whoever wrote the program that opens it.
What one byte holds
A byte distinguishes two hundred and fifty six things, because it is eight switches and each switch doubles the count. That number, and the way it grows, decides a great deal further on in this course.
| bytes | distinct values | enough for | |
|---|---|---|---|
| a flag | 1 | 256 | 1 |
| a letter in one alphabet | 1 | 256 | 1 |
| a colour | 3 | 16777216 | 1 |
| a count of people in a country | 2 | 65536 | 0 |
| an instant to the second | 4 | 4294967296 | 0 |
A file is a count of bytes
When every record in a file is the same width, the size of the file stops being metadata and becomes arithmetic.
| step | what is being worked out | the arithmetic | what comes out | and so | what happened |
|---|---|---|---|---|---|
| 1 | the file as it arrived | 48,240 bytes on disk | 48,240 bytes | the header sits inside that total, so it comes off before anything else | Nothing has been opened yet. This number comes from the directory entry. |
| 2 | the part that is records | 48,240 - 240 | 48,000 bytes | every byte of this is record, and every record is the same width | The 240 byte header is the only stretch of the file that is not a record. |
| 3 | how many records | 48,000 / 48 | 1,000 records | the count is known from two format numbers and one file size | A thousand rows, and not one of them was read to find that out. |
| 4 | what is left over | 48,000 - 48 x 1,000 | 0 bytes | nothing spare, so no record was cut in half | Any leftover at all means the file is damaged or the transfer stopped early, which is the commonest real corruption there is. |
| 5 | where the last record begins | 240 + 48 x 999 | 48,192 bytes in | a jump straight to it, rather than a walk past the 999 in front of it | The same multiplication finds any record by its number, which is why this layout survives where it looks archaic. |
This is also the reason fixed width records survive in places where they look archaic. Finding the ten thousandth record is a multiplication rather than a search, which is a property no format with variable length rows can offer, and the sixth and seventh lessons both come back to it.
The same bytes, read two ways
The single idea to carry out of this lesson is that a run of bytes has no intrinsic meaning, so the question to ask of a confusing file is never what it contains but what agreement it was written under.
raw = bytes([0xC3, 0xA9, 0x74, 0xC3, 0xA9]) # five bytes, nothing more
print(raw.decode("utf-8")) # the modern agreement: three letters
print(raw.decode("latin-1")) # an older one: five letters, two of them odd
print(list(raw)) # no letters at all: 195, 169, 116, 195, 169One practical habit follows. When something arrives broken, look at the bytes before theorising, because the bytes are the only part of the situation that is not somebody's assumption. The next lesson is about how to read them.
Recap
- A file is a run of numbers from zero to two hundred and fifty five, and nothing in that run says what the numbers mean.
- The same bytes read under two different agreements give two different and equally valid answers, which is the single idea the rest of this course is built on.
- The agreement usually lives outside the file, in a name, a header, a column type or somebody's assumption, and most corruption is the agreement being lost rather than the bytes being damaged.
This is the reading half
Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.
The contents