使用 CBOR 编码和解码时出现问题
Issue while encoding and decoding using CBOR
我有以下 class,我想使用 TinyCBOR 对其进行解码和编码。
class Test {
public:
int a;
int b;
float c;
}
我正在做以下编码和解码class:
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 网站上找不到任何 encode/decode 多个值的示例。
文档确实不清楚,缺少示例,但显然,您必须将数据分组到映射或数组中。
#include <iostream>
#include "cbor.h"
using namespace std;
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, array;
cbor_encoder_init(&encoder, buf, sizeof(buf), 0);
cbor_encoder_create_array(&encoder, &array, 3);
cbor_encode_int(&array, t.a);
cbor_encode_int(&array, t.b);
cbor_encode_float(&array, t.c);
cbor_encoder_close_container(&encoder, &array);
// Decode values
CborParser parser;
CborValue it, recursed;
int a;
int b;
float c;
cbor_parser_init(buf, sizeof(buf), 0, &parser, &it);
cbor_value_enter_container(&it, &recursed);
cbor_value_get_int(&recursed, &a);
cbor_value_advance_fixed(&recursed);
cout << "Int A: " << a << endl;
cbor_value_get_int(&recursed, &b);
cbor_value_advance_fixed(&recursed);
cout << "Int B: " << b << endl;
cbor_value_get_float(&recursed, &c);
cbor_value_advance_fixed(&recursed);
cout << "Float C: " << c << endl;
cbor_value_leave_container(&it, &recursed);
return 0;
}
我有以下 class,我想使用 TinyCBOR 对其进行解码和编码。
class Test {
public:
int a;
int b;
float c;
}
我正在做以下编码和解码class:
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 网站上找不到任何 encode/decode 多个值的示例。
文档确实不清楚,缺少示例,但显然,您必须将数据分组到映射或数组中。
#include <iostream>
#include "cbor.h"
using namespace std;
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, array;
cbor_encoder_init(&encoder, buf, sizeof(buf), 0);
cbor_encoder_create_array(&encoder, &array, 3);
cbor_encode_int(&array, t.a);
cbor_encode_int(&array, t.b);
cbor_encode_float(&array, t.c);
cbor_encoder_close_container(&encoder, &array);
// Decode values
CborParser parser;
CborValue it, recursed;
int a;
int b;
float c;
cbor_parser_init(buf, sizeof(buf), 0, &parser, &it);
cbor_value_enter_container(&it, &recursed);
cbor_value_get_int(&recursed, &a);
cbor_value_advance_fixed(&recursed);
cout << "Int A: " << a << endl;
cbor_value_get_int(&recursed, &b);
cbor_value_advance_fixed(&recursed);
cout << "Int B: " << b << endl;
cbor_value_get_float(&recursed, &c);
cbor_value_advance_fixed(&recursed);
cout << "Float C: " << c << endl;
cbor_value_leave_container(&it, &recursed);
return 0;
}