Read more

How to encode or decode quoted-printable strings

Arne Hartherz
December 15, 2015Software engineer at makandra GmbH

E-mails are usually encoded using Quoted Printable Show archive.org snapshot . Here is how to decode or encode such strings.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

You probably know Quoted Printable from e-mail bodies appearing in Rails logs, where =s become =3Ds in URLs, or where long lines are split up and trailed by = after each split.

Decode Quoted Printable

Decoding such strings is actually quite simple using plain Ruby:

"foo=3Dbar".unpack('M')[0]
# => "foo=bar"

Note that unpack will return an array. Our result is the 1st item.

Encode a string as Quoted Printable

If you ever need to encode that manually, pack your input similarly:

["foo=bar"].pack('M')
# => "foo=3Dbar\n"

Note the extra line break, and that you need to wrap your input string into an array.

Posted by Arne Hartherz to makandra dev (2015-12-15 16:14)