U
U
Urbansamurai2016-01-21 15:15:25
Python
Urbansamurai, 2016-01-21 15:15:25

How to convert 24 bit integer to little-endian bytes in Python?

There is an audio sample value in int 24 bit format, which must be converted into a little-endian sequence of 3 bytes (a string of 3 characters).
I want to make a function that would do a universal conversion, regardless of the bit depth. In the meantime, it turns out that if the number is too small, then the output may be 1, or 2 bytes - instead of 3x, and incorrect data is written to WAV. Tried struct - doesn't work with 24 bits. Python version 2.7.
I would appreciate any help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2016-01-21
@Urbansamurai

(Any positive integer less than or equal to 2**24 - 1).to_bytes(3, 'little')

>>> (7777).to_bytes(3, 'little')
b'a\x1e\x00'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question