首页 > 解决方案 > 库 cbor 在转换中引入了一个新值

问题描述

我在node.js上做的这个程序,有异常。当我进行 decodeAllSync 时,我得到一个十进制数字的向量,它的数字比通过使用 encodeAsync 重新转换向量得到的数字少一个。为什么我没有得到相同的向量?谢谢

const cbor = require('cbor');
...
const results = cbor.decodeAllSync(output);
const input = cbor.encodeAsync(results);
console.log(output);
input.then( 
    function (x) {
      var v=new Uint8Array(x);
      console.log(v);
    },
    function () {
      console.log("fail ");
    });

我收到输入的打印输出:

Uint8Array [
  129,
  210,
  132,
  77,
  ...

我收到输出的打印输出:

Uint8Array [
  210,
  132,
  77,
  ...

标签: node.jscbor

解决方案


cbor.decodeAllSync使用 n 个参数返回一个包含 n 个条目的数组。所以你编码的东西[]周围有一对额外的。

> cbor.decodeAllSync(cbor.encode(1))
[ 1 ]
> cbor.decode(cbor.encode(1))
1

推荐阅读