GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing stat545.com — Chapter 12 Character encoding | STAT 545 Chapter 12 Character encoding12.1 Resources
12.2 Translating two blog posts from Ruby to RFor now, this page walks through these two mini-tutorials (written for Ruby), but translated to R: Don’t expect much creativity from me here. My goal is faithful translation. 12.3 What is an encoding?Look at the string The base function Use a character less common in English: Ruby
Now we see that it takes more than one byte to represent Take a look at what a single set of bytes looks like when you try different encodings. Here’s, a string encoded as ISO-8859-1 (also known as “Latin1”) with a special character. Ruby
We’ve confirmed that we have the correct bytes (meaning the same as the Ruby example). What would that string look like interpreted as ISO-8859-5 instead? Ruby
It’s garbled, which is your first tip-off to an encoding problem. Also not all strings can be represented in all encodings: Ruby
In Ruby, apparently that is an error. In R, we just get In the Ruby post, we’ve seen 3 string functions so far. Review and note which R function was used in the translation.
12.4 A three-step process for fixing encoding bugs12.4.1 Discover which encoding your string is actually in.Shhh. Secret: this is encoded as Windows-1252. Ruby’s guess is bad. This is not encoded as UTF-8. R admits it doesn’t know and Advice given in post is to sleuth it out based on where the data came from. With larger amounts of text, each language’s guessing facilities presumably do better than they do here. In real life, all of this advice can prove to be … overly optimistic? I find it helpful to scrutinize debugging charts and look for the weird stuff showing up in my text. Here’s one that shows what UTF-8 bytes look like when erroneously interpreted under Windows-1252 encoding. This phenomenon is known as mojibake, which is a delightful word for a super-annoying phenomenon. If it helps, know that the most common encodings are UTF-8, ISO-8859-1 (or Latin1), and Windows-1252, so that really narrows things down. 12.4.2 Decide which encoding you want the string to beThat’s easy. UTF-8. Done. 12.4.3 Re-encode your string
12.5 How to Get From They’re to They’reMoving on to the second blog post now. 12.5.1 Multi-byte charactersSince we need to represent more than 256 characters, not all can be represented by a single byte. Let’s look at the curly single quote. Ruby
The string has 7 characters, but 9 bytes, because we’re using 3 bytes to represent the curly single quote. Let’s focus just on that. Ruby
One of the most common encoding fiascos you’ll see is this: they’re. Note that the curly single quote has been turned into a 3 character monstrosity. This is no coincidence. Remember those 3 bytes? This is what happens when you interpret bytes that represent text in the UTF-8 encoding as if it’s encoded as Windows-1252. Learn to recognize it. Ruby
Let’s assume this little gem is buried in some large file and you don’t immediately notice. So this string is interpreted with the wrong encoding, i.e. stored as the wrong bytes, either in an R object or in a file on disk. Now what? Let’s review the original, correct bytes vs. the current, incorrect bytes and print the associated strings. 12.5.2 Encoding repairHow do you fix this? You have to reverse your steps. You have a UTF-8 encoded string. Convert it back to Windows-1252, to get the original bytes. Then re-encode that as UTF-8. Ruby
|
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |