2017-03-08 117 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',怎么做? –

相关问题