1

我有下表中提到的字段:

table Blob {
name        : string;
size        : ulong;
data        : [ubyte];
}

并生成以下 API

def BlobStart(builder): builder.StartObject(3)
def BlobAddName(builder, name): builder.PrependUOffsetTRelativeSlot(0, flatbuffers.number_types.UOffsetTFlags.py_type(name), 0)
def BlobAddSize(builder, size): builder.PrependUint64Slot(1, size, 0)
def BlobAddData(builder, data): builder.PrependUOffsetTRelativeSlot(2, flatbuffers.number_types.UOffsetTFlags.py_type(data), 0)
def BlobStartDataVector(builder, numElems): return builder.StartVector(1, numElems, 1)
def BlobEnd(builder): return builder.EndObject()

除此之外,我还有一个二元搅拌bin_data,现在我想将该数据填充到dataBlob 的向量中。怎么做 ?

我有以下代码:

blobName = builder.CreateString(blob_name)

Blob.BlobStartDataVector(builder, len(blob_data))
for i in reversed(range(0, len(blob_data))):
    builder.PrependByte(blob_data[i])   #Error here
blob_bin_data = builder.EndVector(len(blob_data))

Blob.BlobStart(builder)
Blob.BlobAddName(builder, blobName)
Blob.BlobAddSize(builder, 30) #for example size is 30
Blob.BlobAddData(builder, blob_bin_data)
binaryBlob = BlobEnd(builder)

使用上面的代码片段,我收到以下错误:

    builder.PrependByte(blob_data[i])
  File "build/bdist.linux-x86_64/egg/flatbuffers/builder.py", line 544, in PrependByte
  File "build/bdist.linux-x86_64/egg/flatbuffers/builder.py", line 472, in Prepend
  File "build/bdist.linux-x86_64/egg/flatbuffers/builder.py", line 627, in Place
  File "build/bdist.linux-x86_64/egg/flatbuffers/number_types.py", line 148, in enforce_number
TypeError: bad number  for type uint8

寻求帮助,如何将二进制数据提供给字节数组?

4

1 回答 1

0

看来您的blob_data对象实际上并不包含字节。您可以调试它以打印该循环中的字节并确保它适合 [0, 255]

于 2017-03-08T22:07:00.873 回答