So I have the string john
. I pack it into a struct. When I unpack it, how can I print john
? Currently it only prints j
. Same thing if I changed the string to Sammy
or other names with different lengths? I have 2 functions to pack and unpack the struct. This what I don't need to worry about the lengths of the first_name
's. The function can do it for me.
The structure is basically
- user_id (in this case
1
) - first_name (a person first name. This string can be of different lengths. In this case
john
)
My Code
from struct import *
def make_struct(user_id, first_name):
return pack("is", user_id, first_name)
def deconstruct_struct(structure):
return unpack("is", structure)
packed = make_struct(1, "john")
unpacked = deconstruct_struct(packed)
print(unpacked[1])
Current output is:
j