【发布时间】:2018-10-19 10:01:09
【问题描述】:
我有下面的类,我想使用TinyCBOR 对其进行解码和编码。
class Test {
public:
int a;
int b;
float c;
}
我正在对这个类进行以下编码和解码:
int main () {
Test t;
t.a = 10;
t.b = 20;
t.c = 3.30;
// Encode values
uint8_t buf[40];
CborEncoder encoder;
cbor_encoder_init(&encoder, &buf, sizeof(buf), 0);
cbor_encode_int(&encoder, t.a);
cbor_encode_int(&encoder, t.b);
cbor_encode_float(&encoder, t.c);
// Decode values
CborParser parser;
CborValue value;
int a;
int b;
float c;
cbor_parser_init(buf, sizeof(buf), 0, &parser, &value);
cbor_value_get_int(&value, &a);
cout << "Int A: " << a << endl;
cbor_value_get_int(&value, &b);
cout << "Int B: " << b << endl;
cbor_value_get_float(&value, &c);
cout << "Float C: " << c << endl;
return 0;
}
问题是我的程序打印:
A: 10
B: 10
它给出了读取浮点数的错误。
可能是什么问题?
我也尝试添加cbor_value_advance_fixed(&value);,但结果相同。
此外,我在TinyCBOR 站点上找不到任何编码/解码多个值的示例。
【问题讨论】: