Created
August 16, 2013 11:38
-
-
Save andris9/6249156 to your computer and use it in GitHub Desktop.
Decode utf7
This file contains hidden or 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
// npm install iconv iconv-lite | |
var Iconv = require("iconv").Iconv, | |
iconv = require("iconv-lite"); | |
var str = "abc&AOQA9gD8-"; | |
function convertFromUTF7_old(str){ | |
var fromUTF7 = new Iconv("UTF-16", "UTF-8//TRANSLIT//IGNORE") | |
// default string encoding by inbox is binary | |
str = new Buffer(str, "binary").toString("utf-8"); | |
try{ | |
return str.replace(/&([^\-]+)\-/g, function(a, r){ | |
r = (r || "").replace(/,/g, "/"); | |
return fromUTF7.convert(new Buffer(r, "base64")).toString("utf-8"); | |
}); | |
}catch(E){ | |
return str; | |
} | |
}; | |
function convertFromUTF7_new(str){ | |
// default string encoding by inbox is binary | |
str = new Buffer(str, "binary").toString("utf-8"); | |
try{ | |
return str.replace(/&([^\-]+)\-/g, function(a, r){ | |
r = (r || "").replace(/,/g, "/"); | |
return iconv.decode(new Buffer(r, 'base64'), 'utf8'); | |
}); | |
}catch(E){ | |
return str; | |
} | |
}; | |
console.log(convertFromUTF7_old(str)); // abcäöü | |
console.log(convertFromUTF7_new(str)); // abc��� |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment