Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中从dec数到2digit十六进制的列表_Python_List_Hex - Fatal编程技术网

python中从dec数到2digit十六进制的列表

python中从dec数到2digit十六进制的列表,python,list,hex,Python,List,Hex,在python3中,我有一个介于0和255之间的数字列表,如下所示: numbers = [79, 104, 44, 32, 105, 116, 32, 119, 111, 114, 107, 115, 32, 102, 2, 2] 我想将其转换为具有十六进制数的字符串,如: 4f682c20697420776f726b7320660202 我不希望包含“0x”,这一点非常重要:小于a的数字后面应该有一个零 "".join(hex(num)[2:].zfill(2) for num in

在python3中,我有一个介于0和255之间的数字列表,如下所示:

numbers = [79, 104, 44, 32, 105, 116, 32, 119, 111, 114, 107, 115, 32, 102, 2, 2]
我想将其转换为具有十六进制数的字符串,如:

4f682c20697420776f726b7320660202
我不希望包含“0x”,这一点非常重要:小于a的数字后面应该有一个零

 "".join(hex(num)[2:].zfill(2) for num in numbers)
 # '4f682c20697420776f726b7320660202'
使用
hex
获取十六进制表示形式

使用
[2::
删除
0x
部分

使用
str.zfill
确保零作为短的前缀,例如
2

最后通过
连接生成器表达式以获得字符串