Converting between strings and ArrayBuffers

后端 未结 24 1400
慢半拍i
慢半拍i 2020-11-22 04:50

Is there a commonly accepted technique for efficiently converting JavaScript strings to ArrayBuffers and vice-versa? Specifically, I\'d like to be able to write the contents

24条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 05:22

    You can use TextEncoder and TextDecoder from the Encoding standard, which is polyfilled by the stringencoding library, to convert string to and from ArrayBuffers:

    var uint8array = new TextEncoder().encode(string);
    var string = new TextDecoder(encoding).decode(uint8array);
    

提交回复
热议问题