你如何在python数组中访问bool,short,long,float和double?

时间:2014-06-24 21:10:04

标签: python c type-conversion protocol-buffers

我使用protobuf从C传递到python的消息,消息采用以下格式

messageMsg {

  repeated uint32 bool = 2 [packed=true];
  repeated uint32 short = 3 [packed=true];
  repeated uint32 long = 4 [packed=true];
  repeated uint32 float = 5 [packed=true];
  repeated uint32 double = 6 [packed=true];
   }

我希望能够在python中解码数据,所以这就是我用int做的事情

    decoded_data = []
    for i in range(0, len(data)):
        raw_bytes = struct.pack('I', data[i]) #convert to raw bytes
        formatted_entry = struct.unpack("i", raw_bytes)[0] #convert to new format
        decoded_data.append(formatted_entry)

但是现在,如果我用短,长,双,布尔这样做,它将不起作用,因为每个都不是32位。

# This does not work for double
for i in range(0, len(data)/2):
    raw_bytes = struct.pack('d', data[2*i:2*(i+1)]+) #convert to raw bytes

# This does not work for bool
for i in range(0, len(data)*4):
    raw_bytes = struct.pack("?", data[i/4:(i+1)/4])

# This does not work for short
etc...

有没有办法可以将uint32解析为short,bool和double?

由于

0 个答案:

没有答案