我想将一个结构序列化成CBOR,然后打印出来,但是我不知道如何验证打印出来的值是否正确。我使用了CBOR.me,但是每次将输出放在cbor.me中时,它都会报告Out of bytes to decode: 753 + 19 > 753,其中753是提供的CBOR的字节数,无论字节数如何,我都会得到这个错误。不管我是使用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!("");
}发布于 2016-05-17 19:45:53
以下是您的输出和正确的输出:
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您正在以十六进制的形式打印值,但不是将它们填充到两个字符中:
print!("{:02x}", byte);https://stackoverflow.com/questions/37284352
复制相似问题