Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python2.x和Python3.x从命令行打印十六进制数据并重定向到文件_Python_Linux_Hex_Command Line Interface - Fatal编程技术网

使用Python2.x和Python3.x从命令行打印十六进制数据并重定向到文件

使用Python2.x和Python3.x从命令行打印十六进制数据并重定向到文件,python,linux,hex,command-line-interface,Python,Linux,Hex,Command Line Interface,因此,我试图通过将print的输出重定向到文件中来将数据写入文件 在Python2.x中,我是以Python-c“print'\x80'>myFile的方式完成的 使用xxd myFile检查hextump时,正确显示800a(忽略换行符0a) 在python3.x中,我尝试将其作为myFile的python3-c“print('\x80')”> 但是,这次hextump是c280 0a 在尝试了一些变通方法之后,我注意到这只发生在大于或等于(十进制)128的值上 python3-c“打印('\

因此,我试图通过将
print
的输出重定向到文件中来将数据写入文件

在Python2.x中,我是以
Python-c“print'\x80'>myFile
的方式完成的

使用
xxd myFile
检查hextump时,正确显示
800a
(忽略换行符
0a

在python3.x中,我尝试将其作为myFile的
python3-c“print('\x80')”>

但是,这次hextump是
c280 0a

在尝试了一些变通方法之后,我注意到这只发生在大于或等于(十进制)
128
的值上

python3-c“打印('\x7f')”>myFile
正确写入
7f0a


我所寻找的是一种在Python 3.x中这样编写它的方法,同时避免,例如,我自己从十六进制转换为十进制。实际上,这是因为Python 2有两个相似的序列:字符串和Unicode字符串,分别对应于
str
Unicode

因此,在Python 2中,可以对字符串进行编码:

>>> u = unichr(0x80)
>>> u.encode('utf-8')
>>> '\xc2\x80'
但在Python3中,所有字符串都是Unicode字符串,并且
print('\x80')
返回
'\xc2\x80'
,这是
U+0080
Unicode代码点的UTF-8表示