Skip to content

Instantly share code, notes, and snippets.

@t3hmrman
Created November 16, 2014 02:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t3hmrman/72ae227e4d84406fe89d to your computer and use it in GitHub Desktop.
Save t3hmrman/72ae227e4d84406fe89d to your computer and use it in GitHub Desktop.
Simple UTF-8 Quoted Printable decoder
/**
* Decode a UTF-8 quoted printable string into a UTF8 string
*
* @param {string} str - UTF quoted printable string (ex. "=E8=97=A4=E6=A3=AE")
*/
function utf8QuotedPrintableDecode(str) {
// Grab the ascii bytes separated by "=", trim first/last elements if empty
var asciiHex = str.split(/=+/);
if (asciiHex[0] === "") { asciiHex.shift(); }
if (asciiHex[asciiHex.length - 1] === "") { asciiHex.pop(); }
// Convert ascii hex into bytes
var bytes = [];
for (var i = 0; i < asciiHex.length; i++) {
bytes.push(String.fromCharCode(parseInt(asciiHex[i], 16)));
}
// Return UTF8 decoded string (trick outlined @ http://ecmanaut.blogspot.co.uk/2006/07/encoding-decoding-utf8-in-javascript.html)
var utf8String = decodeURIComponent(escape(bytes.join("")));
return utf8String;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment