1
votes

I'm working on audio codecs in Python (yikes) using byte arrays to edit individual byte data from an audio file.

I have a certain encryption in mind that requires me to perform bit wise operations on single bytes stored in the byte-array.

One of those operations is the ~ operator (bitwise NOT) which essentially reverses the bit (b'0001 becomes b'1110).

The problem is when you reference a single element of a byte array, it returns an int (does Python by default consider untyped 8 bit data integers?). Integers in Python are by default signed (I don't think unsigned integers even exist in Python).

When you try to perform bit-wise NOT on a byte in the byte-array, you get the following error:

>>> array[0] = ~array[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: byte must be in range(0, 256)

This is because it expects an unsigned int between 0-255. How do I convert a signed int into an unsigned int such that the bits used to represent both values remain the same?

Cheers

2
I think numpy should provide unsigned ints. I would try np.array(array).astype(np.uint8) - SpghttCd
"I have a certain encryption in mind" - are you writing your own crypto? Please don't write your own crypto. - user2357112 supports Monica
Hahah no... I wouldn't ever try that... trying to emulate something else - jmkmay

2 Answers

2
votes

Use a different operation for bit flipping.

E.g.:1

array[i] = 255 - array[i]

or also:

array[i] = 255 ^ array[i]

will flip all (i.e.: 8) bits.


1 the math behind this can be worked out from two's complement wikipedia page.

-1
votes

The solution is actually remarkably simple after playing around with a binary calculator a little bit.

Just subtract the magnitude of the SIGNED int from 256, to get the value of the UNSIGNED int with the same binary representation.

So,

-23 signed would be 233 unsigned.

Hope this helps anyone else looking for a solution :)

EDIT: For those saying answer is 255 - array[0]. In this case I'm looking for a way to go from post NOT'd int to its unsigned counter part. So I've already performed the bitwise NOT on the integer, now I'm just getting it back to a form that can be inputted into the byte-array.

So in the end it would look something like this:

tmp = ~array[0]
array[0] = 256 + tmp

or

array[0] = 256 - abs(tmp)

This gets me the correct answer :)