Basis Lab

Bytes, Text and File Formats

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.

FIG 1Five bytes, read under five agreements
stepthe agreementwhat the five bytes 72 101 108 108 111 becomeis it valid
1letters in the common Western schemethe word helloyes
2five separate small whole numbers72, 101, 108, 108, 111yes
3four of them as one whole number, then one spare1214606444, and 111 left overyes
4pixel brightnesses in a rowfive grey dots, mid to lightyes
5a musical instrument instructiona note on, of middling pitchyes
5 steps
Every row is a correct reading of the same five numbers, and nothing in those numbers prefers one row over another. The last column is the uncomfortable part of the figure: there is no wrong answer here, only a reading that does not match what the writer intended. That mismatch, rather than any damage to the bytes themselves, is what people are describing when they say a file is corrupted.

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.

FIG 2How many distinct values fit in a given number of bytes
0.00100000000.00200000000.00300000000.00400000000.001.02.33.54.86.0bytes
values a run of n bytes distinguishes
The curve is drawn in millions so that the shape is visible, and it climbs out of the top of the picture almost immediately: three bytes is sixteen million, four is over four thousand million. The practical readings are that one byte is enough for a letter in one alphabet and not for a letter in all of them, that two bytes is enough for most counts and not for a population, and that four is enough for most things until it is suddenly not, which is the subject of the fourth lesson.
FIG 3Widths and what they are enough for
bytesdistinct valuesenough for
a flag12561
a letter in one alphabet12561
a colour3167772161
a count of people in a country2655360
an instant to the second442949672960
The last column is coded 1 for yes and 0 for no. The fourth row is a real and common bug: two bytes counts to sixty five thousand, which is plenty for a town and nothing like enough for a country, and the failure when it arrives is not an error message but a wrong number. The last row is the one with a date on it, since four bytes of seconds runs out in 2038, and the whole of the fourth lesson is about what happens at the end of a range.

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.

FIG 4A file of 48,240 bytes, counted without reading any of it
stepwhat is being worked outthe arithmeticwhat comes outand sowhat happened
1the file as it arrived48,240 bytes on disk48,240 bytesthe header sits inside that total, so it comes off before anything elseNothing has been opened yet. This number comes from the directory entry.
2the part that is records48,240 - 24048,000 bytesevery byte of this is record, and every record is the same widthThe 240 byte header is the only stretch of the file that is not a record.
3how many records48,000 / 481,000 recordsthe count is known from two format numbers and one file sizeA thousand rows, and not one of them was read to find that out.
4what is left over48,000 - 48 x 1,0000 bytesnothing spare, so no record was cut in halfAny leftover at all means the file is damaged or the transfer stopped early, which is the commonest real corruption there is.
5where the last record begins240 + 48 x 99948,192 bytes ina jump straight to it, rather than a walk past the 999 in front of itThe same multiplication finds any record by its number, which is why this layout survives where it looks archaic.
5 steps
Read the third column down and the size of the file has turned into a record count, a damage check and an address, all in bytes and all before a single field is parsed. The fourth row is the one worth keeping as a habit: the zero is what says the file is whole, and it costs one division to look at.

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.

FIG 5One run of bytes, two readings, in a few lines
python
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, 169
All three lines succeed and all three are correct. The first reads the bytes under the agreement they were written with. The second reads them under an older agreement where every byte is one letter, which produces the familiar pair of strange characters where an accented letter should be. The third declines to read them as letters at all. The third lesson of this course is entirely about which of these happens and why, and the exercise worth doing is running these three lines on a file that has gone wrong at work.

One 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

NextCounting in Twos and Sixteens →

The rest of this course

  1. 01Everything Is a Run of Bytesyou are here
  2. 02Counting in Twos and Sixteensopening only
  3. 03Letters as Numbersopening only
  4. 04Whole Numbers, and Where They Stopopening only
  5. 05Numbers With Fractions In Themopening only
  6. 06Dates, Times, and the Day It Movedopening only
  7. 07Rows, Columns and the Separator Problemopening only
  8. 08Making It Smalleropening only

Read alongside