还在用 charCodeAt 那你就 out 了 - 字节前端的文章 - 知乎 https://zhuanlan.zhihu.com/p/350216928
js按位运算符及其妙用
https://www.cnblogs.com/happy1992/p/7064114.html
https://www.zhihu.com/search?type=content&q=%E5%BC%82%E6%88%96%E5%A6%99%E7%94%A8 ^
https://blog.csdn.net/u013291076/article/details/84950188 ~~
1、使用&运算符判断一个数的奇偶
偶数 & 1 = 0
奇数 & 1 = 1
那么0&1=0,1&1=1
(其实就是判断二进制的第0位,把高位部分全部&0或都归0,然后最低位&1,最后得出0001或者0000即0,1)
2,用^快速交换两个整数
x ^= y; // x = x^y
y ^= x; // y = y^(y^x),因为y^y=0,x^0还是x(1还是1,0还是0)
x ^= y; // x^y^x,得y
3,取整
https://blog.csdn.net/tianguiyuyu/article/details/88997899?utm_medium=distribute.pc_relevant.none-task- blog-baidujs_baidulandingword-1&spm=1001.2101.3001.4242
~~ 假设x,结果是-(-(x+1)+1),按位取反,
例如5的二进制表示0000 0000 0000 0000 0000 0000 0000 0101
取反后1111 1111 1111 1111 1111 1111 1111 1010
如果是~5,取反后的二进制数符号位是负,代表着负数,而其对应的值求法是
-1取反(或者取反加一),然后对原值+符号,即0000 0000 0000 0000 0000 0000 0000 0110,得-6
关于js的二进制数组
js ArrayBuffer
https://wangdoc.com/es6/arraybuffer.html
大端小端
https://www.cnblogs.com/gremount/p/8830707.html
https://blog.csdn.net/weixin_30015835/article/details/114389452
计算机电路先处理低位字节,效率比较高,因为计算都是从低位开始的。所以,计算机的内部处理都是小端字节序。
// 假定某段buffer包含如下字节 [0x02, 0x01, 0x03, 0x07]
const buffer = new ArrayBuffer(4);
const v1 = new Uint8Array(buffer);
v1[0] = 2;
v1[1] = 1;
v1[2] = 3;
v1[3] = 7;
// 0是低地址,栈底
// 计算机采用小端字节序
// 所以头两个字节等于258
// 计算第一位过程(2存储在低位,1在高位) 0000 0010 | 0000 0001,当用uInt16View视图时
// 按照计算即 0000 0001 0000 0010 2+2**8 = 2+256 = 258
// 计算第二位过程(3存储在低位,7在高位) 0000 0011 | 0000 0111,当用uInt16View视图时
// 按照计算即 0000 0111 0000 0011 2**8 + 2**9 + 2**10 + 3 = 3+ = 1792+5 = 1795
if (uInt16View[0] === 258) {
console.log('OK'); // "OK"
}
// 赋值运算
uInt16View[0] = 255; // 字节变为[0xFF, 0x00, 0x03, 0x07]
uInt16View[0] = 0xff05; // 字节变为[0x05, 0xFF, 0x03, 0x07]
uInt16View[1] = 0x0210; // 字节变为[0x05, 0xFF, 0x10, 0x02]
ASCII码和base64, js的btoa方法
https://baijiahao.baidu.com/s?id=1735577033729027737&wfr=spider&for=pc
对于中文一般
encodeURIComponent() //将非ACSII码的字符进行utf-8编码
decodeURIComponent() //解码使用
window.btoa(encodeURIComponent('中国'))
ASCII和Unicode总解释
https://zhuanlan.zhihu.com/p/531296925
String.fromCharCode()
The static String.fromCharCode() method returns a string created from the specified sequence of UTF-16 code units.
String.prototype.codePointAt()
The codePointAt() method returns a non-negative integer that is the Unicode code point value at the given position. Note that this function does not give the nth code point in a string, but the code point starting at the specified string index.
Arraybuffer转string
https://blog.csdn.net/u014436243/article/details/119995339
字符串blob对象 转arraybuffer 转字符串
let blob = new Blob(['hh'])
let filereader = new FileReader()
var result
filereader.onload = ()=>{
console.log(result = filereader.result) //readAsArrayBuffer的结果[104,104]
}
filereader.readAsArrayBuffer(blob)
const Decoder = new TextDecoder('utf-8')
Decoder.decode(result) // hh