QDataStream would probably make this easier.