Skip to content

Instantly share code, notes, and snippets.

@akaspin
Created April 22, 2010 01:07
Show Gist options
  • Save akaspin/374673 to your computer and use it in GitHub Desktop.
Save akaspin/374673 to your computer and use it in GitHub Desktop.
var sys = require("sys");
var fs = require("fs");
var decode = function (utftext) {
var string = "";
var i = 0;
var c = c1 = c2 = 0;
while ( i < utftext.length ) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
}
else if((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i+1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
}
else {
c2 = utftext.charCodeAt(i+1);
c3 = utftext.charCodeAt(i+2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
};
fs.readFile('base.html', "binary", function (err, data) {
if (err) throw err;
sys.puts(typeof(data));
fs.writeFile("out.html", decode(data), "utf8", function(err) {});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment