l python data types and --bytes bytearray

bytes和 bytearray

bytes : it can be seen as a set of values (0-256) (binary) sequences of str
ByteArray : can be regarded as a set of values (0-256) (binary) of the sequence list

bytes type

String transfer bytes Type

# 将返回 bytes 类型 b" abc "
bs1 = bytes("abc","utf-8")
# 可以使用字符的16进制字符表达形式
bs2 = bytes('\x61\x62\x63',"utf-8")
# 直接对字符进行编码成二进制形式
bs2 = "abc".encode()

# 16进制字符转为bytes类型
b1 = bytes.fromhex("61 62 63")  #  ==>   b"abc"   "61,62"是两位16进制数组合,该值不能超过 7F ,否则无法对应ASCII表中字符
b1.hex()                 # ===>  '616263'   ASCII码中abc字符对应的16进制数组成的字符串,上面函数的逆运算

# bytes 类型中单个元素可以看做是一个10进制数值类型
print( b1[0] )  # ==>  10进制数,97

Type value into bytes
already said, bytes object can be seen as a set of values (0-256) type (binary) string of bytes objects can be created according to the value

# 传入数值类型可迭代对象
b1 = bytes(range(97,100))               #  ==> b' abc '
b2 = bytes( [97,98,99] )                   #  ==> b' abc '
b3 = bytes( [97] )                             #  ==> b' a '

# 直接传入10进制数值对象而不是可迭代对象,将会生成对应数值字节的空bytes
b4 = bytes(3)         #  b'\x00\x00\x00'   三个空字符的 bytes 

# 通过数值转化将8进制,16进制数字 生成bytes对象
b5 = bytes( [ int("61",16) ] )    #16进制  == > 10 进制 ==> bytes ==>b"a"
b6 = bytes( [ int("61", 8) ] )     # 8进制  == > 10 进制 ==> bytes  ==>b"1"

#  也可利用bytes 对象转化为 10 进制 数值
num = int.from_bytes(b"abc","big")         # "abc"对应的三个字节拼接在一起作为一个二进制数,并计算为10进制数输出
num                    #    ===>   6382179

object may be understood as bytes byte type str, sequence immutable once created, at the same time, the method can be used in string type, suitable for substantially bytes object.
E.g

m = bytes("abc","utf-8")
n = bytearray(" def ","utf-8")
bs = m + n             # bytes 类型的拼接,生成新的bytes对象
bs                          # ===> b"abcdef" 

Other methods similar to the string str, view usage type str

bytearray type

bytearray can be seen as a set of values ​​(0-256) (binary) of the sequence list, which means bytearray individual elements is variable

ba1 = bytearray(range(97,103))
ba1                                  #  bytearay对象,==>  bytearray(b"abcdef" )
ba1[0]                              #   ==>  97  (integer)
ba1[1 :2]                          #  切片 ==> bytearray(b'bcd')

# 赋值,可变bytearray
ba[ 4 ] = 122                    #  122整型对应字符"z",   ==> b"e" --> b"z"
ba                                    #   bytearray(b"abcdzf" )
ba1[1:4] = b"xyz"             #  切片赋值,替换ba1[1:4]的内容, 只有bytes 或bytearray 序列可赋值
ba1                                  #  bytearray(b'axyzef')

bytearay list of target objects similar to a byte, it is possible to use most of the method of the list, it is noted, is a list object character aspects of the operation, the operation is required bytearay bytes and a byte level element, integer or (since the integer value of 0 to 255 is stored in binary form may be used as a single byte of memory, but also belong to a single byte operations)

m.append (100) # ==> 10 by decimal number, add b "d", the value of a single integer value used for incoming

m.extend (b "efg") # ==> bytearray extended object using bytes or bytearray type iterables

Decimal, octal, hexadecimal character conversion

Decimal conversion

According to a hexadecimal character string (no suffix) or decimal values, the following methods may be used when converting bytes each hexadecimal character object generation

# 内置函数
chr(97)      #    ==> "a"
ord("a")     #    ==> 97

#带前缀 0x 
format(97,"#x")                                   # ==> '0x61'
format(97,"#o")                                   # 8进制字符
format(97,"#b")                                   # 2进制字符
#不带前缀
format(97,"X")                                    # ==> int ==> hex_str
format(97,"o")                                    # ==> int ==> oct_str
format(97,"b")                                    # ==> int ==> bin_str

# 3.6+版本使用方法
# f'{255:X}' 和 f'{255:#X}'                   ===> " FF "  和 "0xFF"

# 无前缀
"%x"%10                                     # ==> 'a'
"%o"%10                                     # ==> '12'
# 带前缀                               
"%#x"%10                                     # ==> '0xa'
"%#o"%10                                     # ==> '0o12'

Guess you like

Origin blog.51cto.com/13946938/2428389