我有一个几十个十进制条目的列表。
CP111 = [
[0, 0, 0, 0, 0, 0, 0, 0],
[126, 129, 165, 129, 189, 153, 129, 126],
[126, 255, 219, 255, 195, 231, 255, 126],
[108, 254, 254, 254, 124, 56, 16, 0],
[16, 56, 124, 254, 124, 56, 16, 0],
[56, 124, 56, 254, 254, 124, 56, 124],
[16, 16, 56, 124, 254, 124, 56, 124],
[0, 0, 24, 60, 60, 24, 0, 0],
[255, 255, 231, 195, 195, 231, 255, 255],
[0, 60, 102, 66, 66, 102, 60, 0],
];
我需要将小数转换为十六进制,然后将十六进制格式化为4位并在不加引号的情况下打印出来。 这和我来的差不多:
CP111 = [
[0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0],
[0x3e, 0x41, 0x55, 0x43, 0x55, 0x41, 0x3e, 0x0],
[0x3e, 0x7f, 0x6b, 0x7d, 0x6b, 0x7f, 0x3e, 0x0],
[0x38, 0x7c, 0x3e, 0x1f, 0x3e, 0x7c, 0x38, 0x0],
[0x8, 0x1c, 0x3e, 0x7f, 0x3e, 0x1c, 0x8, 0x0],
[0x8, 0x1c, 0x29, 0x7f, 0x29, 0x1c, 0x8, 0x0],
[0x8, 0x1c, 0x3d, 0x7f, 0x3d, 0x1c, 0x8, 0x0],
[0x0, 0x1c, 0x3e, 0x3e, 0x3e, 0x1c, 0x0, 0x0],
[0xff, 0xe3, 0xc1, 0xc1, 0xc1, 0xe3, 0xff, 0xff],
[0x0, 0x1c, 0x22, 0x22, 0x22, 0x1c, 0x0, 0x0]
];
for char in CP111:
print '[{}],'.format(', '.join(hex(x) for x in char))
我会得到我需要的打印的十六进制,但不是4宽填充格式
我尝试过:
print "{0:#0{1}x}".format('[{}]'.format(', '.join(hex(x) for x in font_char)),4)
print '[{}]'.format(', '.join("0x{:04x}".format(hex(x) for x in font_char)))
...以及其他几行尝试均以语法错误或ValueError结尾:类型为'str'的对象的未知格式代码'x'
也许不可能将其放在一行上。
我需要:
CP111 = [
[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00],
[0x3e, 0x41, 0x55, 0x43, 0x55, 0x41, 0x3e, 0x00],
[0x3e, 0x7f, 0x6b, 0x7d, 0x6b, 0x7f, 0x3e, 0x00],
[0x38, 0x7c, 0x3e, 0x1f, 0x3e, 0x7c, 0x38, 0x00],
[0x08, 0x1c, 0x3e, 0x7f, 0x3e, 0x1c, 0x08, 0x00],
[0x08, 0x1c, 0x29, 0x7f, 0x29, 0x1c, 0x08, 0x00]
.....
请帮助
答案 0 :(得分:0)
这是我在其他人需要指导的情况下所发现的:
print '[{}],'.format(', '.join('0x{:02x}'.format(x) for x in font_char))