Python struct integer overflow masking is deprecated

When create a script with Python, I get warning message Python struct integer overflow masking is deprecated. My script running well, but I dont like this warning and want to remove this. This is my simple code with Python struct integer overflow masking is deprecated warning message :

#!/usr/bin/python
# -*- coding: utf-8 -*-

import struct

ival = 3123456789
cval = struct.pack(">i",ival)

print (cval)

My script Python use  struct module and want to change integer number (ival variable) to char variable (4 byte cval variable). After read documentation in Python, I get information why my script Python show warning message struct integer overflow masking is deprecated. Range integer datatype in Python is $\pm$2147483647. This warning Python struct integer overflow masking is deprecated showed because our input data range in outside integer datatype.

I solve this Python struct integer overflow masking is deprecated with use numpy library.We can use function uint32 (from numpy) to convert integer to unsigned integer datatype. Unsigned integer datatype have range data from 0 to 4294967295. So, we can use this unsigned integer datatype to remove warning message struct integer overflow masking is deprecated when use struct.pack function. This is modified my script to remove Python struct integer overflow masking is deprecated warning message.

#!/usr/bin/python
# -*- coding: utf-8 -*-

import struct
import numpy as np

ival = 3123456789
uival = np.uint32(ival)
cval = struct.pack(">i",uival)

print (cval)

With convert data from integer to unsigned integer, we can remove warning message Python struct integer overflow masking is deprecated. Are you have any method? please share if you have 🙂

Add a Comment

Your email address will not be published. Required fields are marked *