// 字符串进行 UTF-8编码, 返回字节数组exportfunctionstringToByte(str){let bytes =newArray()let len = str.length
let c
for(let i =0; i < len; i++){
c = str.charCodeAt(i)// returns the Unicode value of the character at the specified location.(in decimal format)if(c >=0x010000&& c <=0x10FFFF){
bytes.push(((c >>18)&0x07)|0xF0);
bytes.push(((c >>12)&0x3F)|0x80);
bytes.push(((c >>6)&0x3F)|0x80);
bytes.push((c &0x3F)|0x80);}elseif(c >=0x0800&& c <=0xFFFF){
bytes.push(((c >>12)&0x0F)|0xE0);// 补第一个字节空缺4位(0x0F即取四位,E0表示1110 0000)
bytes.push(((c >>6)&0x3F)|0x80);// 补第二个字节空缺6位
bytes.push((c &0x3F)|0x80);// 补第三个字节空缺6位}elseif(c >=0x0080&& c <=0x07FF){
bytes.push(((c >>6)&0x1F)|0xC0);// 补第一个字节空缺5位(0x1F即取五位,C0表示1100 0000,位移6是因为第二个字节需要补6位)
bytes.push((c &0x3F)|0x80);}else{
bytes.push(c);}}return bytes;}