运行一下不就知道了。。。
其中函数是我搜到的,不知道你怎么写的。
base = [str(x) for x in range(10)] + [ chr(x) for x in range(ord('A'),ord('A')+6)]
def dec2bin(string_num):
num = int(string_num)
mid = []
while True:
if num == 0: break
num,rem = divmod(num, 2)
mid.append(base[rem])
return ''.join([str(x) for x in mid[::-1]])
def hex2dec(string_num):
return int(string_num.upper(), 16)
hexamessage="2F203030325D353D3839"
binarymessage=""
for i in range(0,len(hexamessage),2):
messageByteString = hexamessage[i]+ hexamessage[i+1]
print "messageByteString: ",messageByteString
messageByteDecimalValue = hex2dec(messageByteString)
print "messageByteDecimalValue: ",messageByteDecimalValue
message8to6Bits = dec2bin(messageByteDecimalValue-32)
print "message8to6Bits: ",message8to6Bits
binarymessage += message8to6Bits[2:8]
print "binarymessage: ",binarymessage
binarymessageID_with_type = binarymessage[0:26];
print "binarymessageID_with_type: ",binarymessageID_with_type
messageByteString: 2F
messageByteDecimalValue: 47
message8to6Bits: 1111
binarymessage: 11
binarymessageID_with_type: 11
messageByteString: 20
messageByteDecimalValue: 32
message8to6Bits:
binarymessage: 11
binarymessageID_with_type: 11
messageByteString: 30
messageByteDecimalValue: 48
message8to6Bits: 10000
binarymessage: 11000
binarymessageID_with_type: 11000
messageByteString: 30
messageByteDecimalValue: 48
message8to6Bits: 10000
binarymessage: 11000000
binarymessageID_with_type: 11000000
messageByteString: 32
messageByteDecimalValue: 50
message8to6Bits: 10010
binarymessage: 11000000010
binarymessageID_with_type: 11000000010
messageByteString: 5D
messageByteDecimalValue: 93
message8to6Bits: 111101
binarymessage: 110000000101101
binarymessageID_with_type: 110000000101101
messageByteString: 35
messageByteDecimalValue: 53
message8to6Bits: 10101
binarymessage: 110000000101101101
binarymessageID_with_type: 110000000101101101
messageByteString: 3D
messageByteDecimalValue: 61
message8to6Bits: 11101
binarymessage: 110000000101101101101
binarymessageID_with_type: 110000000101101101101
messageByteString: 38
messageByteDecimalValue: 56
message8to6Bits: 11000
binarymessage: 110000000101101101101000
binarymessageID_with_type: 110000000101101101101000
messageByteString: 39
messageByteDecimalValue: 57
message8to6Bits: 11001
binarymessage: 110000000101101101101000001
binarymessageID_with_type: 11000000010110110110100000
温馨提示:答案为网友推荐,仅供参考