kriszyp / msgpackr

Ultra-fast MessagePack implementation with extension for record and structural cloning / msgpack.org[JavaScript/NodeJS]

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

change pack buffer to string, can't change back?

lalaze opened this issue · comments

commented

Hi, I want to change buffer to string, then change back

const pBuffer = pack('test')

const pText = pBuffer.toString()

const bBuffer = Buffer.from(pText)

console.log(pText, pBuffer.compare(bBuffer)) // �test -1

got this error

Error: Data read, but end of buffer not reached -17

node version v14.20.1
msgpackr version 1.9,2

How can i get the initial text ? Thank!

Do you want a particular character encoding? Most (human-readable) character encodings don't round trip to binary (utf-8, latin). If you want a string that round trips to binary, you probably want to use base64 (hex can also be used, but less efficient):

const pText = pBuffer.toString('base64')
const bBuffer = Buffer.from(pText, 'base64')
commented

Do you want a particular character encoding? Most (human-readable) character encodings don't round trip to binary (utf-8, latin). If you want a string that round trips to binary, you probably want to use base64 (hex can also be used, but less efficient):

const pText = pBuffer.toString('base64')
const bBuffer = Buffer.from(pText, 'base64')

thank,solved my problem