Text in Python 3 is always Unicode and is represented by the str type, and binary data is represented by the bytes type. They cannot be mixed.
Strings can be encoded to bytes, and bytes can be decoded back to strings.
In [1]:
s = 'Hello world!'
print(s)
print("length is", len(s))
In [2]:
us = 'Hello 世界!'
print(us)
print("length is", len(us))
Now encode both strings to bytes.
In [3]:
bs = s.encode('utf-8')
print(bs)
print("length is", len(bs))
In [4]:
bus = us.encode('utf-8')
print(bus)
print("length is", len(bus))
Decode back to strings.
In [5]:
print(bs.decode('utf-8'))
print(bus.decode('utf-8'))
In [6]:
num = 258
print(num.to_bytes(2, "big"))
print(num.to_bytes(2, "little"))
print(num.to_bytes(4, "big"))
print(num.to_bytes(4, "little"))
In [7]:
import struct
Return a bytes object containing the values v1, v2, … packed according to the format string fmt. The arguments must match the values required by the format exactly.
In [8]:
x = 256
print("Network endianess")
print(struct.pack('!h', x))
print("Little endian")
print(struct.pack('<h', x))
print("Big endian")
print(struct.pack('>h', x))
print("Native endianess")
print(struct.pack('=h', x))
In [9]:
bx = struct.pack('!h', x)
print(struct.unpack('!h', bx))
print(struct.unpack('<h', bx))
In [10]:
print(struct.unpack('!h', bx)[0])
print(struct.unpack('<h', bx)[0])