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

Newbie Question: How to unpack a base64 encoded string which encapsulates data #52

Closed
aprilmateo opened this issue Oct 24, 2016 · 6 comments

Comments

@aprilmateo
Copy link

Hi,

We have a service that packs some data (int, int, boolean) and encodes it (base 64 encoding). It spits out the base64 encoded string. Now I am creating a client side javascript that decodes this string. How do I use it using msgpack-lite? I've read that a multi-level msgpack requires a different handling. Please advise.

Cheers!

@kawanet
Copy link
Owner

kawanet commented Oct 24, 2016

Decoding from base64:

msgpack.decode(Buffer.from('pWhlbGxv', 'base64')) // => hello

Encoding to base64:

msgpack.encode("hello").toString('base64') // => pWhlbGxv

@aprilmateo
Copy link
Author

The base64 encoded string has encapsulated data. I actually have a nodejs script that decodes this, and Im trying to convert it to client side js that will work on most browsers. Here's the nodejs code snippet on how it is upacked:

    const strBuffer = new Buffer(base64EncodedString, 'base64');
const unpacker = new msgpack.Unpacker(strBuffer);
const unpackedLong1 = unpacker.unpackInt();
const unpackedLong2 = unpacker.unpackInt();

How can I use msgpack-lite to decode like this?

@kawanet
Copy link
Owner

kawanet commented Oct 25, 2016

var msgpack = require("msgpack-lite");
var strBuffer = new Buffer(base64EncodedString, 'base64');
var unpacked = msgpack.decode(strBuffer);
console.warn(unpacked);

@ValentinBossi
Copy link

ValentinBossi commented Nov 21, 2016

Hi,

In my Java code i have tested the example code from the official msgpack java repository:
MessageBufferPacker packer = MessagePack.newDefaultBufferPacker(); packer .packInt(1) .packString("leo") .packArrayHeader(2) .packString("xxx-xxxx") .packString("yyy-yyyy"); packer.close();

I can decode the first integer in the browser but all other packages not.
How i can decode the other packages?

Cheers!

@kawanet
Copy link
Owner

kawanet commented Nov 21, 2016

Use event mode of the module:

var msgpack = require("msgpack-lite");

var decoder = msgpack.Decoder();
decoder.on("data", function(data) {
  // you'll receive 1, "leo", 2, "xxx-xxxx" and "yyy-yyyy" in order
});
decoder.on("end", function() {
  // 
});
decoder.decode(buf);

@ValentinBossi
Copy link

soo cool:D thank you very much!

@kawanet kawanet closed this as completed Nov 22, 2016
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

3 participants