将整数格式化为十六进制字符串
- 作者: 为了理想而存在
- 来源: 51数据库
- 2022-12-05
问题描述
我需要从随机整数 (0-255) 列表中创建一串十六进制数字.每个十六进制数字应由两个字符表示:5 - 05"、16 - 10"等.
例子:
<块引用>输入:[0,1,2,3,127,200,255],输出:000102037fc8ff
我想出了:
#!/usr/bin/env pythondef format_me(nums):结果 = ""对于我在 nums 中:如果我 <= 9:结果 += "0%x" % i别的:结果 += "%x" % i返回结果打印格式_me([0,1,2,3,127,200,255])
但是,这看起来有点尴尬.有没有更简单的方法?
解决方案
''.join('%02x'%i for i in input)I need to create a string of hex digits from a list of random integers (0-255). Each hex digit should be represented by two characters: 5 - "05", 16 - "10", etc.
Example:
Input: [0,1,2,3,127,200,255], Output: 000102037fc8ff
I've managed to come up with:
#!/usr/bin/env python
def format_me(nums):
result = ""
for i in nums:
if i <= 9:
result += "0%x" % i
else:
result += "%x" % i
return result
print format_me([0,1,2,3,127,200,255])
However, this looks a bit awkward. Is there a simpler way?
解决方案
''.join('%02x'%i for i in input)
推荐阅读
热点文章
Discord.py(重写)on_member_update 无法正常工作
0
Discord.py 在 vc 中获取用户分钟数
0
discord.py 重写 |为我的命令出错
0
Discord.py rewrite 如何 DM 命令?
0
播放音频时,最后一部分被切断.如何解决这个问题?(discord.py)
0
在消息删除消息 Discord.py
0
如何使 discord.py 机器人私人/直接消息不是作者的人?
0
(Discord.py) 如何获取整个嵌入内容?
0
Discord bot 尽管获得了许可,但不能提及所有人
0
Discord.py discord.NotFound 异常
0
