回答編集履歴
5
byte repr 0x00 -> \x00
answer
CHANGED
@@ -62,8 +62,8 @@
|
|
62
62
|
raise NotImplementedError("unknown data type")
|
63
63
|
|
64
64
|
if __name__ == '__main__':
|
65
|
-
print(repr(cbor_encode(0))) # => b'
|
65
|
+
print(repr(cbor_encode(0))) # => b'\x00'
|
66
|
-
print(repr(cbor_encode(23))) # => b'
|
66
|
+
print(repr(cbor_encode(23))) # => b'\x17'
|
67
67
|
print(repr(cbor_encode([1,2,3,4]))) # => b'\x84\x01\x02\x03\x04'
|
68
68
|
```
|
69
69
|
|
4
if/elif で array と number の処理順序入れ替え。
answer
CHANGED
@@ -54,10 +54,10 @@
|
|
54
54
|
return bytes(result)
|
55
55
|
|
56
56
|
def cbor_encode(value):
|
57
|
+
if isinstance(value, int):
|
58
|
+
return cbor_encode_number(value)
|
57
|
-
|
59
|
+
elif isinstance(value, list):
|
58
60
|
return cbor_encode_array(value)
|
59
|
-
elif isinstance(value, int):
|
60
|
-
return cbor_encode_number(value)
|
61
61
|
else:
|
62
62
|
raise NotImplementedError("unknown data type")
|
63
63
|
|
3
例外のコードを訂正
answer
CHANGED
@@ -30,7 +30,7 @@
|
|
30
30
|
elif 256 > num >= 24: # 24..255の範囲
|
31
31
|
return bytes([0x18, num & 0xff]) # 常に2バイト
|
32
32
|
else:
|
33
|
-
raise NotImplementedError
|
33
|
+
raise NotImplementedError("no supported out of number 0..255")
|
34
34
|
```
|
35
35
|
|
36
36
|
※注意。ここでは、理解しやすくするために「数値のリスト」を用いてますが、
|
@@ -59,7 +59,7 @@
|
|
59
59
|
elif isinstance(value, int):
|
60
60
|
return cbor_encode_number(value)
|
61
61
|
else:
|
62
|
-
raise NotImplementedError
|
62
|
+
raise NotImplementedError("unknown data type")
|
63
63
|
|
64
64
|
if __name__ == '__main__':
|
65
65
|
print(repr(cbor_encode(0))) # => b'0x00'
|
2
訂正 23 \x18 -> \x17
answer
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
|Value|CBOR (バイト列) |CBOR len|JSON (文字列)| JSON len|
|
2
2
|
|:--|:--:|--:|:--:|--:|
|
3
3
|
|0| \x00 |1|"0"|1|
|
4
|
-
|23| \
|
4
|
+
|23| \x17 |1| "23"| 2|
|
5
5
|
|[1,2,3,4]|\x84 \x01 \x02 \x03 \x04 |5|"[1,2,3,4]"|9|
|
6
6
|
|
7
7
|
こういうことでしょうか。
|
1
JSON形式のリストでの総バイト数を追記
answer
CHANGED
@@ -40,7 +40,7 @@
|
|
40
40
|
|
41
41
|
## 長さ23迄の配列
|
42
42
|
|
43
|
-
- JSON では、配列の始点終点の [] に2バイト、区切り文字で 要素数-1の3バイト + データ(1234)の4バイト
|
43
|
+
- JSON では、配列の始点終点の [] に2バイト、区切り文字で 要素数-1の3バイト + データ(1234)の4バイト (2+3+4=計9バイト)
|
44
44
|
- CBOR では1バイト目に配列であることの印とその長さの情報を含みます
|
45
45
|
小さな数(0..23)の配列であれば、そのデータサイズは 1+要素数
|
46
46
|
|