当前位置 : 主页 > 网络编程 > JavaScript >

string byte[]

来源:互联网 收集:自由互联 发布时间:2021-06-30
byte[] to string let onBytesToString = function (bytes) { let str = ''; for (let i = 0, len = bytes.length; i len; i++) { let one = bytes[i].toString(2), v = one.match(/^1+?(?=0)/); if (v one.length == 8) { let bytesLength = v[0].length; le
byte[] to string
let onBytesToString = function (bytes) {
    let str = '';
    for (let i = 0, len = bytes.length; i < len; i++) {
        let one = bytes[i].toString(2),
            v = one.match(/^1+?(?=0)/);
        if (v && one.length == 8) {
            let bytesLength = v[0].length;
            let store = bytes[i].toString(2).slice(7 - bytesLength);
            for (let st = 1; st < bytesLength; st++) {
                store += bytes[st + i].toString(2).slice(2);
            }
            str += String.fromCharCode(parseInt(store, 2));
            i += bytesLength - 1;
        } else {
            str += String.fromCharCode(bytes[i]);
        }
    }
    return str;
}
JavaScript string to byte[]
let onStringToBytes = function (str) {
    let charCode, codeArr;
    let ret = [];
    for (let i = 0, len = str.length; i < len; i++) {
        charCode = str.charCodeAt(i);
        codeArr = [];
        do {
            codeArr.push(charCode & 0xFF);
            charCode = charCode >> 8;
        }
        while (charCode);
        ret = ret.concat(codeArr.reverse());
    }
    return ret;
}
网友评论