unsupported operand type(s) for &: 'str' and 'int' python

43 Views Asked by At

I have an lsb steganography function to hide messages that have been modulated into audio. the results of the modulation are binary numbers 1 and 0. when I run the function I get an error:

unsupported operand type(s) for &: 'str' and 'int'

here's my code:

def lsb(bineraudio, binermod):
    # bineraudio = 0000000000000000000000000001100001100110011101000111100101110000011001000110000101110011011010000000000000000000000000000000000001101001011100110110111100110110011011010111000000110100001100010000
    # binermod = 1111000010101111110000000110111110001111110111110100111111100000000000000101000000110000011011111000000000101111010000001110
    for i, bit in enumerate(binermod):
        bineraudio[i] = (bineraudio[i] & 254) | bit
    # Get the modified bytes
    frame_modified = bytes(bineraudio)

    with open('txt/'+'binary.txt', 'w') as file1: 
        file1.write(frame_modified.bin)

# variables binarypass and binaryaudio contain the binary digits 1 and 0.

how do I fix the error or how to insert a message that has been modulated into the song using lsb? i was trying lsb method using this

1

There are 1 best solutions below

3
On

bineraudio and binermod are both strings, like "01010101...", which is how you're able to index them and iterate over them. However, bitwise operations (like &, |, <<, >>, and so on) take numeric values. So, once you get the bit you're interested in (bineraudio[i]), try simply casting it to an int by calling int(bineraudio[i]), so that you can perform the bitwise & with 254, which is of course already an integer. You'll have to do something similar with bit, since it is also a character of a string.

In summary, your lines 4 and 5 should look like this instead:

for i, bit in enumerate(binermod):
    bineraudio[i] = (int(bineraudio[i]) & 254) | int(bit)