number conversion

nicksjacobson at yahoo.com nicksjacobson at yahoo.com
Wed Apr 6 15:01:23 EDT 2005


I'm writing a program to read in raw data from a WAV file, and write it
to an IT file.  I got this to work in C by reading the data into an
array of unsigned chars called "RawDataAry", then converted it to
signed chars by doing the following:

signed char *CharAry = malloc(sizeof(signed char) * frames);
for (i = 0; i < input_frames; i++)
    CharAry[i] = (signed char)(((signed short)WavDataAry[i]) - 128);

It worked.


But when I tried to do this kind of conversion in Python, I got an
OverflowError exception.

First I read the data into an array of unsigned chars:

fmt = str(chunklen) + 'B'
fmtsize = struct.calcsize(fmt)
rawdata = struct.unpack(fmt, s[:fmtsize])
rawdata = list(rawdata)

Then I tried to convert it:

charary = array.array('b')
charary.fromlist(rawdata)

This last line threw the OverflowError exception, "OverflowError:
signed char is greater than maximum."

Is there a way to do this??  Thanks a lot in advance!!

--Nick




More information about the Python-list mailing list