Skip to content

Instantly share code, notes, and snippets.

@7cc
Last active January 13, 2019 00:01
Show Gist options
  • Select an option

  • Save 7cc/1739f20aa9761831aa5587514a4c202c to your computer and use it in GitHub Desktop.

Select an option

Save 7cc/1739f20aa9761831aa5587514a4c202c to your computer and use it in GitHub Desktop.
data:text/plain;charset=sjis,%82%a0
data:text/plain;charset=euc-jp,%a4%a2
data:text/plain;charset=utf8,%E3%81%82
data:text/plain;charset=utf8,%EF%BB%BF%E3%81%82
// URLは勝手にUTF8にエンコードされる <- ブラウザによる模様
// "\u{0030}\u{0042}" === "0B"
data:text/plain;charset=UTF-16,%ff%fe%42%30
data:text/plain;charset=UTF-16,%ff%feB0
data:text/plain;charset=UTF-16,%42%30
data:text/plain;charset=UTF-16,B0
data:text/plain;charset=UTF-16BE,%fe%ff%30%42
data:text/plain;charset=UTF-16BE,%fe%ff0B
data:text/plain;charset=UTF-16BE,%30%42
data:text/plain;charset=UTF-16BE,0B
data:text/plain;charset=utf-16be;base64,MEI=
data:text/plain;charset=utf-8;base64,44GC
// Pythonから取ると楽 'あ'.encode()
btoa("\xE3\x81\x82")
btoa("\u00E3\u0081\u0082")
btoa("あ")
- 大文字小文字は関係ない
- 複数の表記が可能なものが一部にある
- エラーはブラウザーコンソールに表示される
- 優先順位は BOM > charset
UTF8
UTF-8
sjis
Shift_JIS
shift-jis
// あ
// E38182 UTF-8
// 3042 UTF-16 hex
// 12354 UTF-16 decimal (0x3042)
// older
"あ".charCodeAt(0); // 12354
String.fromCharCode(12354); // "あ"
// newer
"あ".codePointAt(0) // 12354
String.fromCodePoint(12354) // "あ"
encodeURI("あ"); // "%E3%81%82"
"\u3042" === "あ";
"\u{3042}" === "あ";
[0xE3, 0x81, 0x82]; // [227, 129, 130]
["e3", "81" ,"82"].map(e=> parseInt(e, 16)); // [227, 129, 130]
[227, 129 ,130].map(e=> e.toString(16)); // ["e3", "81", "82"]
0x3042; // 12354
parseInt(3042, 16); // 12354
12354..toString(16); // "3042"
new TextEncoder().encode("あ"); // [227, 129, 130] (Uint8Array)
var buffer = new ArrayBuffer(3);
var view = new Uint8Array(buffer);
[0xE3, 0x81, 0x82].forEach((e, i)=> {
view[i] = e
});
new TextDecoder().decode(view); // "あ"
// data:text/plain;charset=sjis,%82%a0
var sjisArray = new Uint8Array([0x82, 0xa0])
new TextDecoder("sjis").decode(sjisArray)
// バイナリデータの作成
var buffer = new ArrayBuffer(2)
var sjisArray = new Uint8Array(buffer)
sjisArray[0] = 0x82
sjisArray[1] = 0xa0
// blob
var blob = new Blob([buffer], {type:"text/plain;charset=SHIFT_JIS"})
var url = URL.createObjectURL(blob)
console.log("from blob", url)
// file
var file = new File([buffer], "foo.txt", {
type: "text/plain;charset=SHIFT_JIS",
});
var url = URL.createObjectURL(file)
console.log("from file", url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment