# 3 steps to fix encoding problems in Ruby

DevFeed: [3 steps to fix encoding problems in Ruby](<https://devfeed.tech/articles/3-steps-to-fix-encoding-problems-in-ruby-26262.md>)

Original publisher: [Read original article](<https://www.justinweiss.com/articles/3-steps-to-fix-encoding-problems-in-ruby/>)

Author: Justin Weiss

Published: 2015-09-16T06:59:11Z

Content type: tutorial

Language: en

Sources: [Justin Weiss](<https://devfeed.tech/sources/justin-weiss.md>)

Topics: [Ruby](<https://devfeed.tech/topics/ruby.md>), [Exception](<https://devfeed.tech/topics/exception.md>)

Tags: [array](<https://devfeed.tech/tags/array.md>), [exception](<https://devfeed.tech/tags/exception.md>), [numbers](<https://devfeed.tech/tags/numbers.md>), [ruby](<https://devfeed.tech/tags/ruby.md>)

## AI overview

A Ruby tutorial explains how string encodings relate bytes to characters, why changing an encoding can alter printed text without changing the underlying bytes, and how conversion errors occur. It introduces Ruby methods for converting strings, inspecting bytes, and interpreting bytes under another encoding, then presents a three-step process for fixing encoding bugs.

## Source excerpt

You only really think about a string's encoding when it breaks. When you check your exception tracker and see Encoding::InvalidByteSequenceError: "\xFE" on UTF-8 staring you in the face. Or maybe "they're" starts showing up as "theyâEUR ™re". So, when you have a bad encoding, how do you figure out what broke? And how can you fix it? What is an encoding? If you can imagine what encoding does to a string, these bugs are easier to fix. You can think of a string as an array of bytes, or small numbers: irb(main):001:0> "hello!".bytes => [104, 101, 108, 108, 111, 33] In this encoding, 104 means h, 33 means !, and so on. It gets trickier when you use characters that are less common in English: irb(main):002:0> "hellṏ!".bytes => [104, 101, 108, 108, 225, 185, 143, 33] Now it's harder to tell which number represents which character. Instead of one byte, ṏ is represented by the group of bytes [225, 185, 143]. But there's still a relationship between bytes and characters. And a string's encoding defines that relationship. Take a look at what a single set of bytes looks like when you try different encodings: # Try an ISO-8859-1 string with a special character! irb(main):003:0> str = "hellÔ!".encode("ISO-8859-1"); str.encode("UTF-8") => "hellÔ!" irb(main):004:0> str.bytes => [104, 101, 108, 108, 212, 33] # What would that string look like interpreted as ISO-8859-5 instead? irb(main):005:0> str.force_encoding("ISO-8859-5"); str.encode("UTF-8") => "hellд!" irb(main):006:0> str.bytes => [104, 101, 108, 108, 212, 33] The bytes didn't change. But that doesn't look right at all. Changing the encoding changed how the string printed, without changing the bytes. And not all strings can be represented in all encodings: irb(main):006:0> "hi∑".encode("Windows-1252") Encoding::UndefinedConversionError: U+2211 to WINDOWS-1252 in conversion from UTF-8 to WINDOWS-1252 from (irb):61:in `encode' from (irb):61 from /usr/local/bin/irb:11:in `<main>' Most encodings are small, and can't handle every pos