function for packing and unpacking 2d state