shamblett / cbor

A CBOR implementation for Dart
MIT License
36 stars 15 forks source link

encode int num 43008 decode another num -22528 #43

Closed seal90 closed 2 years ago

seal90 commented 2 years ago

i try to run the code at below, i don't unstand why i encode 43008 but decode another num -22528 var a = cbor.encode(const CborSmallInt(43008)); print(a); // [25, 168, 0] var aa = cbor.decode(a); print(aa); // -22528

and i use cbor: ^5.0.0

shamblett commented 2 years ago

Your using the SmallInt type, this has a length of 2 bytes giving you a signed range of -32768 to +32767, if you want to encode a vlaue larger than 32767 please use int

seal90 commented 2 years ago

if i try encode 430080 by SmallInt when i decode i got true num. the code like this var a = cbor.encode(const CborSmallInt(43008)); print(a); var aa = cbor.decode(a); if(aa is CborSmallInt) { print(aa.toInt()); }

print("-------------"); var a1 = cbor.encode(CborInt(BigInt.from(43008))); print(a1); var aa1 = cbor.decode(a1); if(aa1 is CborSmallInt) { print(aa1.toInt()); }

print("-------------"); var a2 = cbor.encode(const CborSmallInt(430080)); print(a2); var aa2 = cbor.decode(a2); if(aa2 is CborSmallInt) { print(aa2.toInt()); }

and the result is

[25, 168, 0] -22528

[25, 168, 0] -22528

[26, 0, 6, 144, 0] 430080

shamblett commented 2 years ago

Sorry I see the problem now, this has been fixed you need version 5.0.1 of the package, not 5.0.0

seal90 commented 2 years ago

Thinks i change to 5.0.1 the data will true