All files / src/common hex.ts

0% Statements 0/9
100% Branches 0/0
0% Functions 0/3
0% Lines 0/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15                             
export const fromBuffer = (buf: ArrayBuffer): string => {
  return Array.prototype.map.call(
    new Uint8Array(buf), 
    x => ('00' + x.toString(16)).slice(-2) // '00' is for left padding
  ).join('');
}
 
export const toBuffer = (hex: string): ArrayBuffer => {
  const arr = new Uint8Array(hex.length/2)
  for(let i=0; i < arr.length; i++) {
    arr[i] = parseInt(hex.slice(i*2, i*2 + 2), 16)
  }
  return arr.buffer
}