2

In my c1.txt file, i have something like

var0 = '0x000000000000004080'

And in my python mail file:

def getVarFromFile(filename):
  import imp
  f = open(filename)
  global data
  data = imp.load_source('data', '', f)
  f.close()
getVarFromFile('c1.txt') 

The var0 is a 72-bit variable, and in my python file, I want to assign that to 12 variable with 6-bit for each, how can I do that?

Since var0 is a hex variable, seems I can't do

x = int(data.var1) & 0x3F

Thanks

4

1 回答 1

2

我认为您在最后一行中需要做的就是为整数转换提供基础:

values_from_hex = int(data.var1, 16)

然后您可以将该值划分为 6 位值(首先是最低有效位):

six_bit_values = [values_from_hex >> i*6 & 0x3f for i in range(12)]

对于 的值'0x000000000000004080',这得到[0, 2, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0]

于 2013-08-27T13:53:18.030 回答