JavaScript/TypeScript

[TypeScript] ArrayBuffer to String / String to ArrayBuffer

IT공부블로그 2022. 6. 29. 20:20
728x90
반응형
function arrayBufferToString(buffer: ArrayBuffer): string {
  return String.fromCharCode.apply(null, Array.from(new Uint16Array(buffer)));
}

function stringToArrayBuffer(str: string): ArrayBuffer {
  const stringLength = str.length;
  const buffer = new ArrayBuffer(stringLength * 2);
  const bufferView = new Uint16Array(buffer);
  for (let i = 0; i < stringLength; i++) {
    bufferView[i] = str.charCodeAt(i);
  }
  return buffer;
}

출처 : https://gist.github.com/AndrewLeedham/a7f41ac6bb678f1eb21baf523aa71fd5#file-stringtoarraybuffer-ts-L8

 

TS conversion of https://gist.github.com/skratchdot/e095036fad80597f1c1a

TS conversion of https://gist.github.com/skratchdot/e095036fad80597f1c1a - arrayBufferToString.ts

gist.github.com

 

728x90
반응형