Skip to content

Instantly share code, notes, and snippets.

@tkihira
Created July 2, 2012 20:26
Show Gist options
  • Save tkihira/3035490 to your computer and use it in GitHub Desktop.
Save tkihira/3035490 to your computer and use it in GitHub Desktop.
convert SJIS to UTF-8 using AJAX and ecl.js
<!doctype html>
<html><head><title>escape sample</title></head>
// http://www.vector.co.jp/soft/other/java/se342855.html
<script src="ecl.js"></script>
<script>
onload = function() {
var xhr = new XMLHttpRequest();
xhr.overrideMimeType('text/plain; charset=x-user-defined'); // binary
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) { // DONE
if (xhr.status == 200) { // OK
var rt = xhr.responseText;
var len = rt.length;
var binary = new Array(len);
for(var i = 0; i < len; i++) {
binary[i] = rt.charCodeAt(i) & 0xFF; // bitmask needed
}
var a = binary.map(function(element, index, array) {
var str = element.toString(16).toUpperCase();
if (str.length == 1) {
return "%0" + str;
}
return "%" + str;
});
document.body.innerHTML = UnescapeSJIS(a.join(""));
} else {
alert("status = " + xhr.status);
}
}
}
xhr.open("GET", "Shift-JIS.txt");
xhr.send();
};
</script>
</head>
<body></body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment