python data l-bytes and bytearray

Keywords: Python ascii encoding

bytes and bytearray

bytes: Can be seen as a set of numerical (0-256) (binary) str sequences
bytearray: can be seen as a list sequence of values (0-256) (binary)

bytes type

String bytes type

# The bytes type B "abc" will be returned
bs1 = bytes("abc","utf-8")
# You can use the 16-digit character representation of characters
bs2 = bytes('\x61\x62\x63',"utf-8")
# Direct encoding of characters into binary form
bs2 = "abc".encode()

# Conversion of hexadecimal characters to bytes
b1 = bytes.fromhex("61 62 63")  #  ==> B "abc" and "61,62" are combinations of two hexadecimal numbers. The value should not exceed 7F, otherwise it cannot correspond to characters in ASCII tables.
b1.hex()                 # ===> A string of hexadecimal numbers corresponding to the abc character in the'616263'ASCII code. The inverse operation of the above function

# A single element in a bytes type can be considered a 10-digit numeric type
print( b1[0] )  # ==> decimal number, 97

Conversion of numerical values to bytes types
As mentioned earlier, bytes objects can be considered as a set of numeric (0-256) type (binary) strings, so bytes objects can be created from numeric values.

# Iterative Objects of Input Numeric Type
b1 = bytes(range(97,100))               #  ==> b' abc '
b2 = bytes( [97,98,99] )                   #  ==> b' abc '
b3 = bytes( [97] )                             #  ==> b' a '

# Passing directly into a 10-digit numeric object instead of an iterative object generates empty bytes corresponding to numeric bytes
b4 = bytes(3)         #  bytes of b' x00 x00 x00 x00'three empty characters 

# Generating bytes objects from octal and hexadecimal digits by numerical conversion
b5 = bytes( [ int("61",16) ] )    #Hexadecimal=> 10-decimal=> bytes==> B "a"
b6 = bytes( [ int("61", 8) ] )     # 8-digit=> 10-digit=> bytes==> B "1"

#  You can also use bytes objects to convert to 10-digit values
num = int.from_bytes(b"abc","big")         # The three bytes corresponding to "abc" are spliced together as a binary number and computed as 10-digit output.
num                    #    ===>   6382179

Bytes object can be understood as str type of bytes. Once a sequence is created, it is immutable. At the same time, the method that string type can use is basically applicable to bytes object.
for example

m = bytes("abc","utf-8")
n = bytearray(" def ","utf-8")
bs = m + n             # Bytes-type splicing to generate new bytes objects
bs                          # ===> b"abcdef" 

Other string methods are similar to str, so you can see how STR types are used

bytearray type

Bytearray can be seen as a list sequence of values (0-256) (binary), meaning that the individual elements in bytearray are variable.

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

# Assignment, variable bytearray
ba[ 4 ] = 122                    #  122 integers correspond to the character "z", ==> B "e" - -> B "z"
ba                                    #   bytearray(b"abcdzf" )
ba1[1:4] = b"xyz"             #  Slice assignment, replacing the contents of ba1[1:4], only bytes or bytearray sequences can be assigned
ba1                                  #  bytearray(b'axyzef')

The bytearay object is similar to a byte list object, so you can use most of the list method. Note that the list object is a character-level operation, while bytes and bytearay need to operate on byte-level elements, or integers (because integer values of 0-255 can be stored directly in binary form). In a single byte of memory, it also belongs to a single byte operation.

M. append (100) #=> adds B "d" through a decimal number, and single values are passed in using integer values

M. extend (b "efg")#=> Extend bytearray objects using bytes or bytearray-type iterative objects

Character conversion in decimal, octal and hexadecimal systems

Binary conversion

The bytes object can be generated according to the hexadecimal string (without prefix) or the decimal value. The following methods can be used to convert the various decimal characters

# Built-in function
chr(97)      #    ==> "a"
ord("a")     #    ==> 97

#0x with prefix 
format(97,"#x")                                   # ==> '0x61'
format(97,"#o")                                   # Octal character
format(97,"#b")                                   # Binary character
#No prefix
format(97,"X")                                    # ==> int ==> hex_str
format(97,"o")                                    # ==> int ==> oct_str
format(97,"b")                                    # ==> int ==> bin_str

# 3.6 + Version Usage
# f'{255:X}' and f'{255:#X}'===> "FF" and "0xFF"

# No prefix
"%x"%10                                     # ==> 'a'
"%o"%10                                     # ==> '12'
# With prefix                               
"%#x"%10                                     # ==> '0xa'
"%#o"%10                                     # ==> '0o12'

Posted by vwinstead on Sat, 10 Aug 2019 04:20:34 -0700