【问题标题】:nodejs canvas Slowbuffer to stringnodejs画布Slowbuffer到字符串
【发布时间】:2013-11-12 12:06:51
【问题描述】:

我使用节点画布;

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

结果:

<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 ...>

和

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

结果:

<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 ...>

它们不相等:(,所以当我将它存储到redis时,我无法取回它;

【问题讨论】:

  • 请使用代码块进行代码/输出。如果它是你想要的方式,还要检查预览。
  • 你确定这些结果是正确的。它们看起来很混乱。

标签: node.js


【解决方案1】:

根据你想要的格式,可以使用以下方法:

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

继续阅读以了解更多替代方案:https://nodejs.org/api/buffer.html#buffer_buf_tostring_encoding_start_end

【讨论】:

    【解决方案2】:

    您不能只调用 Buffer.toString 并假设一切都会好起来,因为默认编码是 utf8。如果要对二进制数据进行编码,则需要base64编码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-08
      • 2018-05-03
      • 2017-05-28
      • 1970-01-01
      • 1970-01-01
      • 2012-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多