Node Buffers, from utf8 to binary

前端 未结 1 1862
忘掉有多难
忘掉有多难 2020-12-09 22:52

I\'m receiving data as utf8 from a source and this data was originally in binary form (it was a Buffer). I have to convert back this data to a

相关标签:
1条回答
  • 2020-12-09 23:35

    You cannot expect binary data converted to utf8 and back again to be the same as the original binary data because of the way utf8 works (especially when invalid utf8 characters are replaced with \ufffd).

    You have to use another format that correctly preserves the data. This could be 'hex', 'base64', 'binary', or some other binary-safe format provided by a third-party module. Obviously you should probably keep it as a Buffer if you can.

    0 讨论(0)
提交回复
热议问题