我希望在python中实现以下步骤
1)字符串7f33117cf266a525
2)大写字母7F33117CF266A525
3)将其放入数组[7F,33,11,7C,F2,66,A5,25]
4)将其转换为二进制[127,51,17,124,242,102,165,37]
反之亦然
1)binary [127,51,17,124,242,102,165,37]
2)转换为十六进制[7F,33,11,7C,F2,66,A5,25]
3)7F33117CF266A525
4)7f33117cf266a525
string="7f33117cf266a525"
print(string.upper())
T=list(string)
T
给出输出 [“ 7”,“ F”,“ 3”,“ 3”,“ 1”,“ 1”,“ 7”,“ C”,“ F”,“ 2”,“ 6”,“ 6”,“ A','5','2','5'] 如何用逗号分隔两个字符?
答案 0 :(得分:2)
尝试使用此方法将string
中的每两个字符分开:
T = [string[i:i+2] for i in range(0, len(string), 2)]
# T = ['7f', '33', '11', '7c', 'f2', '66', 'a5', '25']
但是,如果string
中的字符数为奇数,并且想要从头开始获取每两个字符的列表,请尝试以下方法:
T = list(map(''.join, zip(*[iter(string)]*2)))
# T = ['7f', '33', '11', '7c', 'f2', '66', 'a5', '25']
区别在于,如果string = '7f33117cf266a5251'
,则第一个列表理解返回['7f', '33', '11', '7c', 'f2', '66', 'a5', '25', '1']
,而第二个仍返回['7f', '33', '11', '7c', 'f2', '66', 'a5', '25']
答案 1 :(得分:2)
除非您知道数据,否则不要真正拆分它
GET
答案 2 :(得分:2)
如果您使用的是Python 3.5+,则可以使用bytes.fromhex
方法将十六进制字符串转换为字节,并使用列表构造函数将字节转换为整数列表:
>>> list(bytes.fromhex('7f33117cf266a525'))
[127, 51, 17, 124, 242, 102, 165, 37]
您可以使用bytes构造函数将整数列表转换为字节,并使用bytes.hex
方法将字节转换为十六进制字符串:
>>> bytes([127, 51, 17, 124, 242, 102, 165, 37]).hex()
'7f33117cf266a525'
答案 3 :(得分:1)
1)字符串"7f33117cf266a525"
string = "7f33117cf266a525"
2)大写"7F33117CF266A525"
string = string.upper()
3)将其放入数组["7F","33","11","7C","F2","66","A5","25"]
string = [string[i:i+2] for i in range(0, len(string), 2)]
4)将其转换为二进制[127,51,17,124,242,102,165,37]
string = [int(x, 16) for x in string]
反之亦然
1)二进制[127,51,17,124,242,102,165,37]
binary = [127,51,17,124,242,102,165,37]
2)将其转换为十六进制["7F","33","11","7C","F2","66","A5","25"]
binary = [hex(x)[2:] for x in binary]
3)字符串"7f33117cf266a525"
(已经小写)
binary = "".join(binary)
答案 4 :(得分:1)
此代码可能有效。
LIKE