How to encode or decode quoted-printable strings

Posted . Visible to the public.

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

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.

Arne Hartherz
Last edit
Arne Hartherz
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2015-12-15 15:14)