python string of bytes and conversion

Source: https://www.cnblogs.com/skiler/p/6687337.html

1, bytes mainly to see the computer, string mainly posters

2, the middle of a bridge is the coding rules, and now the trend is utf8

3, bytes objects is binary, hexadecimal easily converted into, for example, \ x64

4, string content is what we see, for example, 'abc'

5, string encoded encode, converted to binary objects, to identify the computer

6, bytes after the anti-coding decode, converted into a string, let us look at, but pay attention to the anti-coding encoding rules are range, \ range xc8 not identified utf8

7, examples:

import hashlib

Byte b

b = b"example"

String object s

s = "example"
print(b)
print("example")

Byte String to convert

b2 = bytes (s, encoding = 'utf8') # encoding format must be developed

print(b2)

Will encode a string bytes object

= str.encode b3 (s)
b4 = s.encode ()
print (b3)
print (type (b3))
print (b4)

Byte decode and obtain a target subject str

s2 = bytes.decode(b)
s3 = b.decode()
print(s2)
print(s3)

Guess you like

Origin www.cnblogs.com/shengulong/p/11079291.html