0

我想将一个结构序列化为 CBOR 并将其打印出来,但是我不知道如何验证打印的值是否正确。我使用了 CBOR.me,但每次我将输出放在 cbor.me 中时,它都会报告提供的 CBOR 的字节数在Out of bytes to decode: 753 + 19 > 753哪里753,无论字节数如何,我都会收到此错误。无论我使用serde_cbor::to_vec, 还是serde_cbor::to_vec_sd.

#![feature(custom_derive, plugin)]
#![plugin(serde_macros)]

extern crate serde;
extern crate serde_cbor;


#[derive(Deserialize, Serialize)]
struct Points {
    x: u8,
    y: u8,
}


fn main() {
    let points = Points {x: 1, y: 1};
    let cbor = serde_cbor::to_vec(&points);

    for byte in cbor {
        print!("{:x}", byte);
    }

    println!("");
}
4

1 回答 1

2

这是您的输出和正确的输出:

a2 61 78 16 17 91
a2 61 78 01 61 79 01

你看到问题了吗?

a2 61 78  1 61 79  1
a2 61 78 01 61 79 01

您将值打印为十六进制,但不将它们零填充为 2 个字符:

print!("{:02x}", byte);
于 2016-05-17T19:45:53.310 回答