Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.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
如何在python中创建在for循环期间更改值的字节变量?_Python_Unicode_Utf 8_Byte_Utf 16 - Fatal编程技术网

如何在python中创建在for循环期间更改值的字节变量?

如何在python中创建在for循环期间更改值的字节变量?,python,unicode,utf-8,byte,utf-16,Python,Unicode,Utf 8,Byte,Utf 16,我想为\x00\x00\x00\x00\x00\x00到\xff\xff\xff\xff范围内的所有字符打印utf-8和utf-16之间的差异,我想这样做: for i in range (0x10): h = hex(i)[2:3] byte = b"\x00\x00\x00\x0{i}" print(byte.decode('utf-16', 'ignore')) print(byte.decode('utf-8', 'ignore')) 其中,字节变量中的i

我想为
\x00\x00\x00\x00\x00\x00
\xff\xff\xff\xff
范围内的所有字符打印utf-8和utf-16之间的差异,我想这样做:

for i in range (0x10):
  h = hex(i)[2:3]
  byte = b"\x00\x00\x00\x0{i}"

print(byte.decode('utf-16', 'ignore'))
print(byte.decode('utf-8', 'ignore'))
其中,字节变量中的i在循环期间发生变化,并使用嵌套循环覆盖字节变量中的所有字节。
有可能这样做吗?

在python中,存在另一种将所有字节从
\x00\x00\x00\x00\x00
打印到
\xff\xff\xff
的方法?

struct模块是构建字节字符串的好工具。在这里,您可以通过这种方式生成您的:

for i in range(0x10):
    b = struct.pack('>I', i)   # convert an int to a big endian 4 bytes string
    print(b)
正如预期的那样:

b'\x00\x00\x00\x00'
b'\x00\x00\x00\x01'
b'\x00\x00\x00\x02'
b'\x00\x00\x00\x03'
b'\x00\x00\x00\x04'
b'\x00\x00\x00\x05'
b'\x00\x00\x00\x06'
b'\x00\x00\x00\x07'
b'\x00\x00\x00\x08'
b'\x00\x00\x00\t'
b'\x00\x00\x00\n'
b'\x00\x00\x00\x0b'
b'\x00\x00\x00\x0c'
b'\x00\x00\x00\r'
b'\x00\x00\x00\x0e'
b'\x00\x00\x00\x0f'

struct
模块是构建字节字符串的好工具。在这里,您可以通过这种方式生成您的:

for i in range(0x10):
    b = struct.pack('>I', i)   # convert an int to a big endian 4 bytes string
    print(b)
正如预期的那样:

b'\x00\x00\x00\x00'
b'\x00\x00\x00\x01'
b'\x00\x00\x00\x02'
b'\x00\x00\x00\x03'
b'\x00\x00\x00\x04'
b'\x00\x00\x00\x05'
b'\x00\x00\x00\x06'
b'\x00\x00\x00\x07'
b'\x00\x00\x00\x08'
b'\x00\x00\x00\t'
b'\x00\x00\x00\n'
b'\x00\x00\x00\x0b'
b'\x00\x00\x00\x0c'
b'\x00\x00\x00\r'
b'\x00\x00\x00\x0e'
b'\x00\x00\x00\x0f'

实现40多亿个组合。这需要大量打印。要知道有40多亿个组合。那是很多印刷品。