Questions tagged [python-bytearray]

38 questions
13
votes
1 answer

How to decode base64 String directly to binary audio format

Audio file is sent to us via API which is Base64 encoded PCM format. I need to convert it to PCM and then WAV for processing. I was able to decode -> save to pcm -> read from pcm -> save as wav using the following code. decoded_data =…
ddd
  • 4,665
  • 14
  • 69
  • 125
11
votes
1 answer

Does slicing a bytes object create a whole new copy of data in Python?

Say I have very large bytes object (after loading binary file) and I want to read parts by parts and advance the starting position until it meets the end. I use slicing to accomplish this. I'm worried that python will create completely new copy each…
Tekz
  • 1,279
  • 14
  • 20
9
votes
3 answers

Is it possible to effectively initialize bytearray with non-zero value?

I need to have huge boolean array. All values should be initialized as "True": arr = [True] * (10 ** 9) But created as above it takes too much memory. So I decided to use bytearray for that: arr = bytearray(10 ** 9) # initialized with zeroes Is…
Mikhail M.
  • 5,588
  • 3
  • 23
  • 31
7
votes
1 answer

How to implement readinto() method

I would like to implement a class which inherits from RawIOBase. I'm struggling to implement the readinto method. Basically, I don't know how to alter the content of the bytearray object passed as a parameter. I've tried following (naive)…
Luke
  • 71
  • 1
  • 2
5
votes
1 answer

Python 3.73 inserting into bytearray = "object cannot be re-sized"

I'm working with a bytearray from file data. I'm opening it as 'r+b', so can change as binary. In the Python 3.7 docs, it explains that a RegEx's finditer() can use m.start() and m.end() to identify the start and end of a match. In the question…
rdtsc
  • 1,044
  • 10
  • 17
5
votes
1 answer

Append string to bytearray

I have a byte array, arr and a hexadecimal number a: arr = bytearray() a = 'FE' How can I append this number to bytearray to have the same value, FE? I tried with print(int(a, 16)), but it seems to be a bad idea (it prints 254 instead of FE).
yak
  • 3,770
  • 19
  • 60
  • 111
3
votes
2 answers

TypeError: 'bytearray' object cannot be interpreted as an integer

I want to send audio data over HTTP, but I don't understand why I'm getting this exception: Exception happened during processing of request from ('127.0.0.1', 59976) Traceback (most recent call last): File "/usr/lib/python3.6/socketserver.py",…
valioiv
  • 393
  • 2
  • 5
  • 15
2
votes
2 answers

Numpy array: get the raw bytes without copying

I am trying to concatenate the bytes of multiple Numpy arrays into a single bytearray to send it in an HTTP post request. The most efficient way of doing this, that I can think of, is to create a sufficiently large bytearray object and then write…
2
votes
2 answers

Python string contains byte array

I've experimented with the Python module named array a bit. It has got a way to encode arrays to strings. >>>from array import…
Uncle Dino
  • 812
  • 1
  • 7
  • 23
1
vote
1 answer

Compare every 3rd value inside a python dictionary and slice them

I have the following python dictionary with bytearray as values test_bytearray_dict= {'test_array0': bytearray(b'0000000000000000'), 'test_array1': bytearray(b'0000000001000000'), 'test_array2': bytearray(b'0000000002000000'), 'test_array3': …
1
vote
0 answers

Python decode bytearray

I need send some data over serialport, but I have problem with bytearray I wrote simple script, like this: msg = bytearray([255,1,134,0,0,0,0,121]) print(msg) But output from this looks like this: python3…
sosnus
  • 968
  • 10
  • 28
1
vote
1 answer

python string decode displayed as byte array in list context

Why is this string printing as a byte array in the context of a list, printing as expected in a print statement, and the type is of string, not bytearray? stringList = [] # Comparing a string, and a string decoded from a byte array theString =…
Joe
  • 1,014
  • 1
  • 13
  • 28
1
vote
1 answer

How to store PagingState in Cassandra Python driver?

I'm implementing a REST API which behind the scenes queries Cassandra (via Python driver) and returns the result. Now the items to be queried will be huge so I want to have pagination capability. The ResultSet returned by execute() method has a…
1
vote
1 answer

Behavior of byte arrays indexing in python 3

Run across this: import sys; print('Python %s on %s' % (sys.version, sys.platform)) Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)] on win32 b'\n' == b'\n' True # well obviously b'\n'[-1] == b'\n' False #…
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
1
vote
1 answer

Faster bytearray list comprehension / conversion?

I have a bytearray of 2^18 (262144) bytes. The goal is to convert it to a numpy array of complex values. Packing within the bytearray is: [0:1] Imaginary_0 16bit [2:3] Real_0 16bit [4:5] Imaginary_1 16bit [6:7] Real_1 16bit ........ [262140:262141]…
Borisw37
  • 739
  • 2
  • 7
  • 30
1
2 3