python字节流的转换

python字节流的转换
Page content

你会需要它的

0x00 关键内建函数

> ord

ord(c, /)
	'''
    返回一个单字符字符串的Unicode代码点
    '''

    >>> ord('A')
    65

> hex

hex(number, /)
	'''
    返回整数的十六进制表示形式
	'''

    >>> hex(65)
    '0x41'

> bin

bin(number, /)
	'''
    返回整数的二进制表示形式
    '''

    >>> bin(65)
	'0b1000001'

> chr

chr(i, /)
	'''
    返回一个字符的Unicode字符串,其序号为i 
    i范围: 0 <= i <= 0x10ffff
    '''

    >>> chr(65)
    'A'

> bytearray


class bytearray(object)
   # bytearray(iterable_of_ints) -> bytearray
   # bytearray(string, encoding[, errors]) -> bytearray
   # bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer
   # bytearray(int) -> bytes array of size given by the parameter initialized with null bytes
   # bytearray() -> empty bytes array
 
   # Construct a mutable bytearray object from:
   #   - an iterable yielding integers in range(256)
   #   - a text string encoded using the specified encoding
   #   - a bytes or a buffer object
   #   - any object implementing the buffer API.
   #   - an integer

   >>> list(bytearray(b'A'))
   [65]

> b'\x'

	>>> bytes([0x41])
	b'A'

> utf16

	>>> 'A'.encode('utf16')
	b'\xff\xfeA\x00'