histelheim
histelheim

Reputation: 5088

In Ruby - what are encodings?

I'm trying to understand what encodings in Ruby are - there are lots of articles about encodings, such as this one and this one. However, none of them explain the basic question a newbie might have - what is an encoding in the first place?

Upvotes: 1

Views: 90

Answers (2)

Esailija
Esailija

Reputation: 140210

What is meant by character encoding here is a system of describing how computers represent characters in binary.

In UTF-8 encoding, the character ä is represented as 1100 0011 1010 0100, or 0xC3 0xA4 in hexadecimal.

In Windows-1252 encoding, the same character is represented as 1110 0100 or 0xE4 in hexadecimal.

So let's say you tell a computer to read a file in Windows-1252, but the file is actually encoded as UTF-8. The file contains just one character, say ä. Since the file is in UTF-8, the file actually contains the bits 0xC3 0xA4. Now because you told (implicitly or explicitly) the computer to read the file in Windows-1252, you will actually see ä instead of ä.

Upvotes: 5

Ry-
Ry-

Reputation: 224905

An encoding is a means of transforming some sequence of bytes into text. ASCII is one where there's a single byte per character. UTF-8 is another common one that uses a variable number of bytes to encode a somewhat larger set of characters. And, of course, Character Encoding on Wikipedia is probably a helpful read.

Upvotes: 4

Related Questions