I have read samples out of a wave file using the wave module, but it gives the samples as a string, it's out of wave so it's little endian (for example, \x00
).
What is the easiest way to convert this into a python integer, or a numpy.int16 type? (It will eventually become a numpy.int16, so going directly there is fine).
Code needs to work on little endian and big endian processors.
Kevin Burke's answer to this question works great when your binary string represents a single short integer, but if your string holds binary data representing multiple integers, you will need to add an additional 'h' for each additional integer that the string represents.
For Python 2
Convert Little Endian String that represents 2 integers
Output: (1024, 1281)
Convert Little Endian String that represents 3 integers
Output: (1024, 1281, 1027)
Obviously, it's not realistic to always guess how many "h" characters are needed, so:
Output: (1024, 1281, 1027)
For Python 3
Output: (1024, 1281, 1027)
The
struct
module converts packed data to Python values, and vice-versa."h" means a short int, or 16-bit int. "<" means use little-endian.
struct
is fine if you have to convert one or a small number of 2-byte strings to integers, butarray
andnumpy
itself are better options. Specifically, numpy.fromstring (called with the appropriatedtype
argument) can directly convert the bytes from your string to an array of (whatever thatdtype
is). (Ifnumpy.little_endian
is false, you'll then have to swap the bytes -- see here for more discussion, but basically you'll want to call thebyteswap
method on the array object you just built withfromstring
).