Python struct

def Bin2Hex(hash):
hexchars = []
for i in struct.unpack('%dB' % (len(hash),), hash):
hexchars.append('%02x' % (i,))
return ''.join(hexchars)

python的struct.unpack('50s')解析定长为50的二进制buff,但这个50是我计算并存储在一个变量中怎么办
str_bytes_num = 50
st = struct.unpack('%ds' % str_bytes_num, buf)

参考链接

http://docs.python.org/library/struct.html

http://wenku.baidu.com/view/6726f20a52ea551810a68738.html

备注关键点:
1.一个空的元组由一对空的圆括号组成,如myempty = ()。然而,含有单个元素的元组就不那么简单了。你必须在第一个(唯一一个)项目后跟一个逗号,这样Python才能区分元组和表达式中一个带圆括号的对象。即如果你想要的是一个包含项目2的元组的时候,你应该指明singleton = (2 , )。
2.L[0] 取得的是元素 L[0:1] 取得是列表 错误的将L[0:1]当元素使用了

原文地址:https://www.cnblogs.com/moonflow/p/2434202.html