Created
November 16, 2014 02:26
-
-
Save t3hmrman/72ae227e4d84406fe89d to your computer and use it in GitHub Desktop.
Simple UTF-8 Quoted Printable decoder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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