how to convert arraybuffer to string

前端 未结 4 787
伪装坚强ぢ
伪装坚强ぢ 2021-01-14 21:26

I have written a simple TCP server on node.js to send some data to a Chrome app. In the chrome app, when I get the data, I convert that to string using below function, I get

4条回答
  •  太阳男子
    2021-01-14 22:05

    The modern (Chrome 38+) way to do this would be, assuming the encoding is UTF-8:

    var decoder = new TextDecoder("utf-8");
    
    function ab2str(buf) {
        return decoder.decode(new Uint8Array(buf));
    }
    

    This uses the TextDecoder API; see documentation for more options, such as a different encoding.

    See also: Easier ArrayBuffer<->String conversion with the Encoding API @ Google Developers

提交回复
热议问题