Python-所有值为0到2^24的十六进制文本文件
我正在尝试创建一个包含0到2^24的所有可能值的十六进制文件 这就是我到目前为止所做的:Python-所有值为0到2^24的十六进制文本文件,python,Python,我正在尝试创建一个包含0到2^24的所有可能值的十六进制文件 这就是我到目前为止所做的: file_name = "values.txt" counter = 0 value = 0x0000000000 with open (file_name, 'w') as writer: while counter < 16777216: data_to_write = str(value) + '\n' writer.write(data_to_writ
file_name = "values.txt"
counter = 0
value = 0x0000000000
with open (file_name, 'w') as writer:
while counter < 16777216:
data_to_write = str(value) + '\n'
writer.write(data_to_write)
counter = counter + 1
value = value + 0x0000000001
file\u name=“values.txt”
计数器=0
值=0x0000000000
以open(文件名“w”)作为编写器:
当计数器<16777216时:
数据写入=str(值)+'\n'
writer.write(数据写入)
计数器=计数器+1
值=值+0x0000000001
这是我想要的,但用整数。有没有一种简单的方法可以将其转换为文件中的十六进制值(仍然是字符串)
谢谢在编写时只需使用字符串的格式化方法:
writer.write("{:#X}".format(data_to_write))
例如:
>>> "{:#x}".format(123324)
'0x1e1bc'
>>> "{:#X}".format(123324)
'0X1E1BC'
谢谢你的帮助 它现在正在工作:
file_name = "value.txt"
counter = 0
value = 0x0000000000
with open (file_name, 'w') as writer:
while counter < 10000:
data_to_write = str(hex(value)[2:].zfill(6)) + '\n'
writer.write(data_to_write)
counter = counter + 1
value = value + 0x0000000001
file\u name=“value.txt”
计数器=0
值=0x0000000000
以open(文件名“w”)作为编写器:
当计数器<10000时:
数据写入=str(十六进制(值)[2:].zfill(6))+'\n'
writer.write(数据写入)
计数器=计数器+1
值=值+0x0000000001
“2^2的所有可能值”-您必须解释您的意思。对于2^2,我只看到两个可能的值:^为XOR时为0,如果^为幂运算时为4。你似乎想说些完全不同的话。我试过:data_to_write=str(hex(value))+'\n'但我先得到0x,所以我需要以某种方式删除它…0到2^24:-)对不起,你是说2^24-1吗?@Matrilx然后试着hex(value)[2:
。你可以使用“{:010x}”。格式(value)
,以提高可读性。