-3

I use node-canvas;

....
var content = canvas.toBuffer();
var length=content.length;
console.log(content);

result:

<SlowBuffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 30 00 00 00 30 08 06 00 00 00 57 02 f9 87 00 00 00 06 62 4b 47 44 00 ff 00 ff 00 ff a0 bd a7 93 ...>

And

var buf=new SlowBuffer(length);
buf.write(content.toString());
console.log(buf);

result:

<Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 00 28 00 28 00 00 ff db 00 43 00 08 06 06 07 06 05 08 07 07 07 09 09 08 0a 0c 14 0d 0c 0b 0b 0c 19 12 13 0f 14 ...>

They are not equal:( ,so when I store it into redis ,I can't get it back;

2
  • Please use code blocks for code/output. Also check the preview if it is the way you want.
    – user568109
    Nov 1, 2013 at 16:01
  • Are you sure those results are correct. They look jumbled.
    – user568109
    Nov 1, 2013 at 16:06

2 Answers 2

0

Depending on the format you want, you can use the following methods:

buf.toJSON() // Straight to JSON format
buf.toString('utf8') ; // UTF8 format

Read on for more alternatives: https://nodejs.org/api/buffer.html#buffer_buf_tostring_encoding_start_end

-1

You can't just call Buffer.toString and assume that everything will be all right, since the default encoding is utf8. If you want to encode binary data, you need base64 encoding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.