Text files and encodings
A file holds bytes, and an encoding says which bytes are which characters. This lesson shows the bytes of one text in UTF-8, UTF-16, Latin-1 and ASCII, the byte order mark, how to read a file in the right encoding, and how a program can find out which encoding a file has.
What you will learn#
- the bytes of a text in four encodings,
- the byte order mark (BOM),
- what
File.WriteAllTextandFile.ReadAllTextdo about encodings, - reading a file that has no BOM and an old encoding (Latin-1),
- strict decoding with
UTF8Encoding(False, True), - a simple order of tests to detect an encoding,
- Windows and Unix line endings.
The tutorial Unicode and encodings explained characters, code points and the .NET string. This lesson is about files: the bytes on the disk.
The program#
# text files and encodings - the bytes of a text, the byte order mark, reading a file in the right encoding, and strict decoding
load "dotnet.ring"
using {
"System"
"System.IO"
"System.Text"
}
func main
oFile = new File()
oPath = new Path()
cFolder = oPath.Combine([ oPath.GetTempPath(), "ringdotnet-enc-" + oPath.GetRandomFileName() ])
new Directory().CreateDirectory(cFolder)
# A file holds BYTES. An ENCODING says which bytes mean which characters. The text "Café €" has an accent and a euro sign, which
# are not in the old 7-bit ASCII table.
cText = "Caf" + char(195) + char(169) + " " + char(226) + char(130) + char(172) # the UTF-8 bytes of Café €
oUtf8 = new Encoding().UTF8
oUtf16 = new Encoding().Unicode # .NET calls UTF-16 (little endian) "Unicode"
oLatin1 = new Encoding().Latin1
oAscii = new Encoding().ASCII
# 1. The same text in four encodings: different bytes, different sizes
? "1. the bytes of the text 'Cafe' with an accent, a space and the euro sign:"
for aItem in [ ["UTF-8",oUtf8], ["UTF-16",oUtf16], ["Latin-1",oLatin1], ["ASCII",oAscii] ]
cBytes = aItem[2].GetBytes(cText)
? " " + padRight(aItem[1],8) + padRight("" + len(cBytes) + " bytes",10) + toHex(cBytes)
next
? " UTF-8 uses 1 byte for the letters of English and 2 to 4 for the others; UTF-16 uses 2 or 4; Latin-1 has one byte for 256"
? " characters and no euro sign (it becomes ?); ASCII has 128 characters."
# 2. The byte order mark (BOM): a few bytes at the start of a file that say which encoding it has. UTF-8 does not need one, and many
# programs write none; UTF-16 files have one almost always, because it also tells the order of the two bytes of a character.
? nl + "2. the byte order marks:"
oFile.WriteAllText([ :path = cFolder + "/a.txt", :contents = cText ])
? " File.WriteAllText with no encoding writes UTF-8 and no BOM: first bytes " + toHex(left(oFile.ReadAllBytes(cFolder + "/a.txt"),3))
oFile.WriteAllText([ :path = cFolder + "/e.txt", :contents = cText, :encoding = (new UTF8Encoding).@New(True) ])
? " with UTF8Encoding(True) the file starts with " + toHex(left(oFile.ReadAllBytes(cFolder + "/e.txt"),3)) + " (the BOM of UTF-8)"
oFile.WriteAllText([ :path = cFolder + "/b.txt", :contents = cText, :encoding = oUtf16 ])
? " with the UTF-16 encoding the file starts with " + toHex(left(oFile.ReadAllBytes(cFolder + "/b.txt"),2)) + " and has " + len(oFile.ReadAllBytes(cFolder + "/b.txt")) + " bytes (the BOM FF FE: little endian)"
# 3. Reading. File.ReadAllText looks at the BOM and uses the right encoding by itself: the UTF-16 file reads back as the same text.
? nl + "3. File.ReadAllText finds the BOM:"
? " the UTF-8 file: " + yesNo(oFile.ReadAllText(cFolder + "/a.txt") = cText)
? " the UTF-16 file: " + yesNo(oFile.ReadAllText(cFolder + "/b.txt") = cText)
? " the UTF-16 file, with UTF-8 given as the encoding: " + yesNo(oFile.ReadAllText([ :path = cFolder + "/b.txt", :encoding = oUtf8 ]) = cText) + " (a BOM in the file wins over the given encoding)"
# 4. A file without a BOM and in an old encoding. The bytes of Latin-1 "Café" are 43 61 66 E9. Read as UTF-8 the byte E9 is not valid
# and becomes the replacement character; read as Latin-1 the text is right. Nothing in the file tells which: the program or the
# user must know. (Windows programs write old files in the code page of the computer: Latin-1 is a good first guess for Europe.)
oFile.WriteAllBytes(cFolder + "/c.txt","Caf" + char(233))
cAsUtf8 = oFile.ReadAllText(cFolder + "/c.txt")
cAsLatin1 = oFile.ReadAllText([ :path = cFolder + "/c.txt", :encoding = oLatin1 ])
? nl + "4. a Latin-1 file without BOM:"
? " read as UTF-8: " + len(cAsUtf8) + " bytes, the last character is the replacement sign (EF BF BD): " + yesNo(right(cAsUtf8,3) = char(239) + char(191) + char(189))
? " read as Latin-1: " + yesNo(cAsLatin1 = "Caf" + char(195) + char(169)) + " (the same text as 'Cafe' with the accent)"
# 5. Strict decoding: UTF8Encoding(False, True) raises an error for bytes that are not valid UTF-8, instead of replacing them. That
# is a good test: "is this file UTF-8?". A text that is valid in UTF-8 and has accents is almost certainly UTF-8.
? nl + "5. is it valid UTF-8?"
? " the UTF-8 file: " + yesNo(isValidUtf8(oFile.ReadAllBytes(cFolder + "/a.txt")))
? " the Latin-1 file: " + yesNo(isValidUtf8(oFile.ReadAllBytes(cFolder + "/c.txt")))
? " a plain English file is valid UTF-8 and valid Latin-1: " + yesNo(isValidUtf8("Hello"))
# 6. A program that must read a file from outside decides in this order: a BOM says it; else valid UTF-8 says UTF-8; else it uses a
# default (Latin-1 or the encoding the user chose). And it writes UTF-8, with a BOM only for programs that need it.
? nl + "6. detecting the encoding of the three files:"
for cName in [ "a.txt", "e.txt", "b.txt", "c.txt" ]
? " " + cName + ": " + detect(oFile.ReadAllBytes(cFolder + "/" + cName))
next
# 7. Line endings: Windows files use two characters (13 10), Unix files one (10). ReadAllLines understands both.
oFile.WriteAllBytes(cFolder + "/d.txt","one" + char(13) + char(10) + "two" + char(10) + "three")
aLines = oFile.ReadAllLines(cFolder + "/d.txt").toList()
? nl + "7. a file with mixed line endings has " + len(aLines) + " lines: " + aLines[1] + ", " + aLines[2] + ", " + aLines[3]
new Directory().Delete([ :path = cFolder, :recursive = True ])
# does the text of bytes decode without a replacement as UTF-8? (the strict encoding raises an error for bad bytes)
func isValidUtf8 cBytes
oStrict = (new UTF8Encoding).@New([ :encoderShouldEmitUTF8Identifier = False, :throwOnInvalidBytes = True ])
try
oStrict.GetString(cBytes)
return True
catch
return False
done
# the order of the tests
func detect cBytes
if left(cBytes,3) = char(239) + char(187) + char(191)
return "UTF-8 with a BOM"
but left(cBytes,2) = char(255) + char(254)
return "UTF-16 little endian (the BOM FF FE)"
but left(cBytes,2) = char(254) + char(255)
return "UTF-16 big endian (the BOM FE FF)"
but isValidUtf8(cBytes)
return "UTF-8 (no BOM)"
ok
return "not UTF-8: try Latin-1"
# the bytes of a text as hexadecimal pairs
func toHex cBytes
cText = ""
for i = 1 to len(cBytes)
cText += right("0" + upper(hex(ascii(substr(cBytes,i,1)))),2) + " "
next
return trim(cText)
func yesNo lValue
if lValue
return "yes"
ok
return "no"
func padRight cText,nWidth
return cText + copy(" ",nWidth - len(cText))1. the bytes of the text 'Cafe' with an accent, a space and the euro sign:
UTF-8 9 bytes 43 61 66 C3 A9 20 E2 82 AC
UTF-16 12 bytes 43 00 61 00 66 00 E9 00 20 00 AC 20
Latin-1 6 bytes 43 61 66 E9 20 3F
ASCII 6 bytes 43 61 66 3F 20 3F
UTF-8 uses 1 byte for the letters of English and 2 to 4 for the others; UTF-16 uses 2 or 4; Latin-1 has one byte for 256
characters and no euro sign (it becomes ?); ASCII has 128 characters.
2. the byte order marks:
File.WriteAllText with no encoding writes UTF-8 and no BOM: first bytes 43 61 66
with UTF8Encoding(True) the file starts with EF BB BF (the BOM of UTF-8)
with the UTF-16 encoding the file starts with FF FE and has 14 bytes (the BOM FF FE: little endian)
3. File.ReadAllText finds the BOM:
the UTF-8 file: yes
the UTF-16 file: yes
the UTF-16 file, with UTF-8 given as the encoding: yes (a BOM in the file wins over the given encoding)
4. a Latin-1 file without BOM:
read as UTF-8: 6 bytes, the last character is the replacement sign (EF BF BD): yes
read as Latin-1: yes (the same text as 'Cafe' with the accent)
5. is it valid UTF-8?
the UTF-8 file: yes
the Latin-1 file: no
a plain English file is valid UTF-8 and valid Latin-1: yes
6. detecting the encoding of the three files:
a.txt: UTF-8 (no BOM)
e.txt: UTF-8 with a BOM
b.txt: UTF-16 little endian (the BOM FF FE)
c.txt: not UTF-8: try Latin-1
7. a file with mixed line endings has 3 lines: one, two, threeThis program uses a generic function (dnNew) where a class cannot be used. The lesson The generic functions explains them.
How it works#
Bytes and encodings#
The text Café € has six characters. In UTF-8 it has 9 bytes: the letters of English take 1 byte each, é takes 2 and € takes 3. In UTF-16 (called Unicode by .NET) each character takes 2 bytes here: 12 bytes. Latin-1 has one byte for each of 256 characters, so the text takes 6 bytes, and the euro sign, which Latin-1 does not have, becomes ?. ASCII has only 128 characters, so é and € both become ?. A byte[] is a Ring text of bytes in this package, and the function toHex shows each byte with ascii and hex.
The byte order mark#
A BOM is a few bytes at the start of a file that say which encoding it has: EF BB BF for UTF-8, FF FE for UTF-16 little endian (and FE FF for big endian). UTF-8 has no byte order problem, so most programs write no BOM, and File.WriteAllText writes none. Some programs (older Windows tools) need it, and UTF8Encoding(True) writes it. UTF-16 files almost always have it.
Reading#
File.ReadAllText looks at the first bytes. When it finds a BOM it uses that encoding, even when you give another one: the UTF-16 file reads back correctly in step 3. Without a BOM it uses the encoding you give, and UTF-8 when you give none.
A file without a BOM#
The bytes of a Latin-1 file Café are 43 61 66 E9. E9 alone is not valid UTF-8, so a UTF-8 reader replaces it with the replacement character (EF BF BD) and the accent is lost for ever. Nothing in the file tells that it is Latin-1: the program or the user has to know. Windows programs of older times wrote the code page of the computer, and Latin-1 is a good first guess for Western Europe.
Strict decoding#
UTF8Encoding(False, True) has a second value that makes it raise an error for bytes that are not valid UTF-8, instead of replacing them. Wrapping GetString in try gives the function isValidUtf8. A text with accents that is valid UTF-8 is almost certainly UTF-8: the other encodings produce byte patterns that are very unlikely to be valid UTF-8 by chance. A plain English file is valid in all of them, so it does not matter which one you choose.
The order of the tests#
detect follows the usual order: (1) a BOM says the encoding; (2) valid UTF-8 means UTF-8; (3) otherwise use a default and let the user change it. Real programs add a menu Encoding for the last case. And a program that writes files should write UTF-8, with a BOM only for the programs that need it.
Line endings#
Windows uses two characters at the end of a line (13 10), Unix and macOS use one (10). File.ReadAllLines understands both and also a mix, so the file here has three lines. When a program writes a file for another system it chooses the line ending explicitly.
What you learned#
- the bytes of a text in four encodings,
- the BOM and its effect on reading,
- the loss of data when the encoding is wrong,
- strict decoding and a way to detect UTF-8,
- the two kinds of line ending.
Exercises#
- Write a text with the characters
üandßin UTF-8 and in Latin-1 and compare the sizes. - Write
convertFile(source, target, fromEncoding, toEncoding)that changes the encoding of a file. - Make a function that counts how many replacement characters a text has, and use it to check a file after reading.
- Read a Windows file with
ReadAllLinesand write it back with Unix line endings (join the lines withchar(10)).