Python 不会打印 NOP
Python will not print NOP
我正在尝试开发 shell 代码来进行二进制开发练习。我需要打印到文件的指令之一是 NOP (0x90)。无论出于何种原因,每当我尝试打印出该字符时,我的 Python 环境(运行ning on Ubuntu 20.04)就会挂起。换句话说,我不能 运行 任何其他行 Python 代码,除非我 Ctrl-D 退出环境并再次使用 python 命令。其他不可打印的字符不会发生这种情况。为了向您展示我的意思,这里有一个例子:
$ python3
Python 3.8.2 (default, Apr 27 2020, 15:53:34)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print('\x55')
U
>>> print('\x80') # Python doesn't hang with other weird characters
>>> print('\x91')
>>> print('\x89')
>>> print('\x90') # Python does not respond after this line
此外,如果我尝试在 shell 中使用此命令,我会得到不同的结果。它无需挂起即可工作,但 0xc2 与 0x90 一起打印出来。
$ python3 -c "print('\x90')" > test
$ xxd test
00000000: c290 0a ...
有没有办法在不挂起或添加 0xc2 字符的情况下 Python 将 0x90 字符打印到文件?
以二进制模式打开文件并使用 write
而不是 print
。
在'\x90'前加一个b
使其成为字节串
所以我最终找到了一种无需打开文件即可打印内容的方法。
$ python -c "import sys; sys.stdout.buffer.write(b'\x90')" > test
$ xxd test
00000000: 90
我正在尝试开发 shell 代码来进行二进制开发练习。我需要打印到文件的指令之一是 NOP (0x90)。无论出于何种原因,每当我尝试打印出该字符时,我的 Python 环境(运行ning on Ubuntu 20.04)就会挂起。换句话说,我不能 运行 任何其他行 Python 代码,除非我 Ctrl-D 退出环境并再次使用 python 命令。其他不可打印的字符不会发生这种情况。为了向您展示我的意思,这里有一个例子:
$ python3
Python 3.8.2 (default, Apr 27 2020, 15:53:34)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print('\x55')
U
>>> print('\x80') # Python doesn't hang with other weird characters
>>> print('\x91')
>>> print('\x89')
>>> print('\x90') # Python does not respond after this line
此外,如果我尝试在 shell 中使用此命令,我会得到不同的结果。它无需挂起即可工作,但 0xc2 与 0x90 一起打印出来。
$ python3 -c "print('\x90')" > test
$ xxd test
00000000: c290 0a ...
有没有办法在不挂起或添加 0xc2 字符的情况下 Python 将 0x90 字符打印到文件?
以二进制模式打开文件并使用 write
而不是 print
。
在'\x90'前加一个b
使其成为字节串
所以我最终找到了一种无需打开文件即可打印内容的方法。
$ python -c "import sys; sys.stdout.buffer.write(b'\x90')" > test
$ xxd test
00000000: 90