Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unpacking packed data from string #13

Open
emilsoman opened this issue Jul 26, 2015 · 1 comment
Open

Unpacking packed data from string #13

emilsoman opened this issue Jul 26, 2015 · 1 comment

Comments

@emilsoman
Copy link

Seems like msgpack.decode assumes the encoded data is an instance of ArrayBuffer. Is there a way to unpack a JS string object containing packed data ? For example:

var packed = "\x81\xA3foo\xA3bar";
msgpack.unpack(packed); // => {foo: 'bar'}
@creationix
Copy link
Owner

Yes, the bodec library that's used can easily do this. Assuming your string is encoded as "raw" encoding, the fromRaw utility is what you want. https://github.com/creationix/bodec/blob/master/bodec-browser.js#L200-L210

Note that strings in JavaScript are unicode and not 8-bit arrays. In particular, they are 16-bit arrays (UCS-16). The "raw" encoding is when you treat each 16-bits as a single byte and ignore the higher 8 bits. JavaScript strings normally encode as UTF-8 when converting to raw 8-bit byte arrays so be careful when working with "raw" strings that use the right encoder/decoder when encoding to-from bytes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants