学学nodejs小码农养成记

nodejs 十六进制字符串型数据与btye型数据相互转换

2017-03-09  本文已影响1910人  F_wind

nodejs,ES6

/**
 * byte型转换十六进制
 * @param b
 * @returns {string}
 * @constructor
 */
const Bytes2HexString = (b)=> {
    let hexs = "";
    for (let i = 0; i < b.length; i++) {
        let hex = (b[i]).toString(16);
        if (hex.length === 1) {
            hexs = '0' + hex;
        }
        hexs += hex.toUpperCase();
    }
    return hexs;
}
 /**
 * 十六进制转换btye型
 * @param str
 * @returns {Promise}
 */
const Hexstring2btye = (str)=> {
    let pos = 0;
    let len = str.length;
    if (len % 2 != 0) {
        return null;
    }
    len /= 2;
    let hexA = new Array();
    for (let i = 0; i < len; i++) {
        let s = str.substr(pos, 2);
        let v = parseInt(s, 16);
        hexA.push(v);
        pos += 2;
    }
    return hexA;
}

持续更新,敬请期待...

上一篇下一篇

猜你喜欢

热点阅读