【发布时间】:2016-12-03 20:43:14
【问题描述】:
我正在尝试在 python 2.7 中为 cmd 编写一个十六进制查看器
它几乎可以正常工作,但是如果我尝试在 Windows 上查看已编译的文件,它只会显示其中的一小部分。我已经想通了,read() 在0x1a(ASCII 格式)的第一次出现时中断。 Notepad++ 将此字符显示为 SUB。我不知道这个控制字符是做什么的,为什么read() 会停在这个字符上,以及如何避免这个中断。谁能帮帮我?
这是我的全部代码:
def main():
while True:
print "Enter a file path:"
path = raw_input()
f = open(path, 'r')
text = f.read() # seems to break at 0x1a/SUB
f.close()
for c in text:
hex_c = hex(ord(c))[2:]
if len(hex_c) % 2: # if the hex number consists of 1 digit
hex_c = '0' + hex_c # fill the string with a zero
print hex_c,
print # just as a line break in the console
if __name__ == '__main__':
main()
【问题讨论】:
标签: python python-2.7