2017-03-08 118 views
0

我有以下表中提到的字段:如何從python中的flatbuffer的二進制字符串ubyte矢量提供數據?

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() 

而且除了我有一個二進制stirng bin_data,現在我想這些數據填充到data矢量Blob。怎麼做 ?

我有下面這段代碼:

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 

尋求幫助,如何將二進制數據提供給字節數組?

回答

0

看起來您的blob_data對象實際上並不包含字節。你可以調試它打印該循環中的字節,並確保它適合[0,255]

+0

我看到所有的值都在[0,255]。 –

+0

我看到當我將bin_data轉換爲arr = bytearray(bin_data)並遍歷它的工作原理,但我想將二進制數據複製到'data',怎麼做? –

相關問題