<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 String vs. Bytes
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.
Step1:  Now encode both strings to bytes.
Step2:  Decode back to strings.
Step3:  Big Endian vs Little Endian
Step4:  struct package
This module performs conversions between Python values and C structs represented as Python bytes objects.
Step5:  struct.pack(fmt, v1, v2, …)
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.
"!" means network endianess (big endian)
"<" means little endian
">" means big endian
"=" means native
"h" measn short integer (2 bytes)
Step6:  struct.unpack(fmt, buffer)
Unpack from the buffer buffer (presumably packed by pack(fmt, ...)) according to the format string fmt. The result is a tuple even if it contains exactly one item. The buffer’s size in bytes must match the size required by the format,
<ASSISTANT_TASK:>
Python Code:
s = 'Hello world!'
print(s)
print("length is", len(s))
us = 'Hello 世界!'
print(us)
print("length is", len(us))
Explanation: String vs. Bytes
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.
End of explanation
bs = s.encode('utf-8')
print(bs)
print("length is", len(bs))
bus = us.encode('utf-8')
print(bus)
print("length is", len(bus))
Explanation: Now encode both strings to bytes.
End of explanation
print(bs.decode('utf-8'))
print(bus.decode('utf-8'))
Explanation: Decode back to strings.
End of explanation
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"))
Explanation: Big Endian vs Little Endian
End of explanation
import struct
Explanation: struct package
This module performs conversions between Python values and C structs represented as Python bytes objects.
End of explanation
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))
Explanation: struct.pack(fmt, v1, v2, …)
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.
"!" means network endianess (big endian)
"<" means little endian
">" means big endian
"=" means native
"h" measn short integer (2 bytes)
End of explanation
bx = struct.pack('!h', x)
print(struct.unpack('!h', bx))
print(struct.unpack('<h', bx))
print(struct.unpack('!h', bx)[0])
print(struct.unpack('<h', bx)[0])
Explanation: struct.unpack(fmt, buffer)
Unpack from the buffer buffer (presumably packed by pack(fmt, ...)) according to the format string fmt. The result is a tuple even if it contains exactly one item. The buffer’s size in bytes must match the size required by the format,
End of explanation
<END_TASK>