Javascript ArrayBuffer to Hex

后端 未结 9 542
迷失自我
迷失自我 2020-11-30 05:51

I\'ve got a Javascript ArrayBuffer that I would like to be converted into a hex string.

Anyone knows of a function that I can call or a pre written function already

9条回答
  •  春和景丽
    2020-11-30 06:27

    I use this to hexdump ArrayBuffers the same way that Node dumps Buffers.

    function pad(n: string, width: number, z = '0') {
        return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
    }
    function hexdump(buf: ArrayBuffer) {
        let view = new Uint8Array(buf);
        let hex = Array.from(view).map(v => this.pad(v.toString(16), 2));
        return ``;
    }
    

    Example (with transpiled js version):

    const buffer = new Uint8Array([ 4, 8, 12, 16 ]).buffer;
    console.log(hexdump(buffer)); // 
    

提交回复
热议问题