diff --git a/util.js b/util.js new file mode 100644 --- /dev/null +++ b/util.js @@ -0,0 +1,29 @@ +export function str2utf8(s){ + let res=[]; + let c=s.codePointAt(0); + for(let i=0;c!==undefined;i++,c=s.codePointAt(i)){ + console.log(c); + if(c<0x80){res.push(c);} + else if(c<0x800){ + res.push(0b11000000|(c>>>6)); + res.push(0b10000000|(c&0b111111)); + } + else if(c<0x10000){ + res.push(0b11100000|(c>>>12)); + res.push(0b10000000|((c>>>6)&0b111111)); + res.push(0b10000000|(c&0b111111)); + } + else{ + res.push(0b11110000|(c>>>18)); + res.push(0b10000000|((c>>>12)&0b111111)); + res.push(0b10000000|((c>>>6)&0b111111)); + res.push(0b10000000|(c&0b111111)); + } + } + return res; +} + +console.log(str2utf8("$").map(x=>x.toString(16))); +console.log(str2utf8("¢").map(x=>x.toString(16))); +console.log(str2utf8("€").map(x=>x.toString(16))); +console.log(str2utf8("𐍈").map(x=>x.toString(16)));