bytearray

Convert PIL Image to byte array?

Convert PIL Image to byte array? Question: I have an image in PIL Image format. I need to convert it to byte array. img = Image.open(fh, mode=’r’) roiImg = img.crop(box) Now I need the roiImg as a byte array. Asked By: Evelyn Jeba || Source Answers: Thanks everyone for your help. Finally got it resolved!! …

Total answers: 4

Convert binary string to bytearray in Python 3

Convert binary string to bytearray in Python 3 Question: Despite the many related questions, I can’t find any that match my problem. I’d like to change a binary string (for example, “0110100001101001”) into a byte array (same example, b”hi”). I tried this: bytes([int(i) for i in “0110100001101001”]) but I got: b’x00x01x01x00x01′ #… and so on …

Total answers: 3

Convert variable-sized byte array to a integer/long

Convert variable-sized byte array to a integer/long Question: How can I convert a (big endian) variable-sized binary byte array to an (unsigned) integer/long? As an example, ‘x11x34′, which represents 4404 Right now, I’m using def bytes_to_int(bytes): return int(bytes.encode(‘hex’), 16) Which is small and somewhat readable, but probably not very efficient. Is there a better (more …

Total answers: 2

How to split a byte string into separate bytes in python

How to split a byte string into separate bytes in python Question: Ok so I’ve been using python to try create a waveform image and I’m getting the raw data from the .wav file using song = wave.open() and song.readframes(1), which returns : b’x00x00x00x00x00x00′ What I want to know is how I split this into …

Total answers: 3

Byte Array to Hex String

Byte Array to Hex String Question: I have data stored in a byte array. How can I convert this data into a hex string? Example of my byte array: array_alpha = [ 133, 53, 234, 241 ] Asked By: Jamie Wright || Source Answers: Using str.format: >>> array_alpha = [ 133, 53, 234, 241 ] …

Total answers: 5

Python: bytearray vs array

Python: bytearray vs array Question: What is the difference between array.array(‘B’) and bytearray? from array import array a = array(‘B’, ‘abc’) b = bytearray(‘abc’) a[0] = 100 b[0] = ‘d’ print a print b Are there any memory or speed differences? What is the preferred use case of each one? Asked By: Ecir Hana || …

Total answers: 6

How to create a bytes or bytearray of given length filled with zeros in Python?

How to create a bytes or bytearray of given length filled with zeros in Python? Question: All the solutions I found were for lists. Thanks. Asked By: Yan || Source Answers: This will give you 100 zero bytes: bytearray(100) Or filling the array with non zero values: bytearray([1] * 100) Answered By: Ned Batchelder For …

Total answers: 2

Convert python long/int to fixed size byte array

Convert python long/int to fixed size byte array Question: I’m trying to implement RC4 and DH key exchange in python. Problem is that I have no idea about how to convert the python long/int from the key exchange to the byte array I need for the RC4 implementation. Is there a simple way to convert …

Total answers: 10

Python 3 Building an array of bytes

Python 3 Building an array of bytes Question: I need to build a tcp frame with raw binary data, but all examples and tutorials I’ve found talking about bytes always involve conversion from a string, and that’s not what I need. In short, I need to build just an array of bytes: 0xA2 0x01 0x02 …

Total answers: 6

An efficient way of making a large random bytearray

An efficient way of making a large random bytearray Question: I need to create a large bytearry of a specific size but the size is not known prior to run time. The bytes need to be fairly random. The bytearray size may be as small as a few KBs but as large as a several …

Total answers: 4