bug fix of python

here’s the program:

import binascii

file = open(“nibbles.txt”, “r”)

count = 1

bytes = “”

message = “”

for i, line in enumerate(file):

startIndex = line.index(‘(‘)

endIndex = line.index(‘)’)

co = line[startIndex:endIndex+1] # (-1.35, 2.30)

coordinates = line[startIndex+1:endIndex] # -1.35, 2.30

comma = coordinates.index(‘,’)

iVoltage = coordinates[0:comma]

iv = float(iVoltage)

qVoltage = coordinates[comma+2:]

qv = float(qVoltage)

if iv >= 0 and iv <= 2 and qv >= 0 and qv <= 2:

point = “1101”

if iv >= 2 and iv <= 4 and qv >= 0 and qv <= 2:

point = “1001”

if iv >= 0 and iv <= 2 and qv >= 2 and qv <= 4:

point = “1100”

if iv >= 2 and iv <= 4 and qv >= 2 and qv <= 4:

point = “1000”

if iv >= 0 and iv <= 2 and qv <= 0 and qv >= -2:

point = “1111”

if iv >= 2 and iv <= 4 and qv <= 0 and qv >= -2:

point = “1011”

if iv >= 0 and iv <= 2 and qv <= -2 and qv >= -4:

point = “1110”

if iv >= 2 and iv <= 4 and qv <= -2 and qv >= -4:

point = “1010”

if iv >= -4 and iv <= -2 and qv >= 0 and qv <= 2:

point = “0001”

if iv >= -2 and iv <= 0 and qv >= 0 and qv <= 2:

point = “0101”

if iv >= -4 and iv <= -2 and qv >= 2 and qv <= 4:

point = “0000”

if iv >= -2 and iv <= 0 and qv >= 2 and qv <= 4:

point = “0100”

if iv >= -4 and iv <= -2 and qv >= -2 and qv <= 0:

point = “0011”

if iv >= -2 and iv <= 0 and qv >= -2 and qv <= 0:

point = “0111”

if iv >= -4 and iv <= -2 and qv >= -4 and qv <= -2:

point = “0010”

if iv >= -2 and iv <= 0 and qv >= -4 and qv <= -2:

point = “0110”

bytes = bytes + str(point)

if count%2 == 0 :

n = int(bytes, 2)

letter = binascii.unhexlify(‘%x’ %n)

#letter = n.to_bytes((n.bit_length() + 7) // 8, ‘big’).decode() # for Python 3.2+

print(co + ” decodes as ” + point + ” – together, ” + bytes + ” gives “” + letter + “”n”)

bytes = “”

message = message + letter

else :

print(co + ” decodes as ” + bytes)

count += 1

print(“Decoded message with symbol errors included: ” + message)

and the nipples.txt:

Nibble 0: (-0.78, 2.38)

Nibble 1: (-2.56, -3.35)

Nibble 2: (-0.90, -3.25)

Nibble 3: (3.28, 0.84)

Nibble 4: (-0.84, -3.08)

Nibble 5: (-3.10, -1.50)

Nibble 6: (-0.92, -2.87)

Nibble 7: (-1.17, 1.07)

Nibble 8: (-2.92, -3.14)

Nibble 9: (-2.97, 3.51)

Nibble 10: (-0.91, -2.71)

Nibble 11: (3.19, 1.37)

Nibble 12: (-1.11, -0.97)

Nibble 13: (-3.10, -0.80)

Nibble 14: (-2.82, -2.83)

Nibble 15: (-2.55, 3.36)

Nibble 16: (-0.80, -2.88)

Nibble 17: (0.89, 3.16)

Nibble 18: (-0.99, -3.51)

Nibble 19: (-1.22, 1.30)

Nibble 20: (-1.16, -0.86)

Nibble 21: (-3.19, -2.01)

Nibble 22: (-0.58, -1.42)

Nibble 23: (-2.91, -0.80)

Nibble 24: (-2.87, -2.76)

Nibble 25: (-2.72, 2.91)

Nibble 26: (-1.31, -3.36)

Nibble 27: (-1.46, 0.84)

Nibble 28: (-1.09, -0.87)

Nibble 29: (3.53, 3.05)

Nibble 30: (-1.29, -0.99)

Nibble 31: (-2.91, 2.86)

Nibble 32: (-1.42, -2.77)

Nibble 33: (-0.41, 0.98)

Nibble 34: (-1.09, -2.74)

Nibble 35: (1.06, -2.88)

Nibble 36: (-1.21, -1.03)

Nibble 37: (-2.86, -1.03)

Nibble 38: (-1.19, -2.61)

Nibble 39: (2.97, 0.94)

Nibble 40: (-0.51, -1.20)

Nibble 41: (-1.00, -3.00)

Nibble 42: (-1.00, -3.57)

Nibble 43: (-1.30, 1.40)

Nibble 44: (-3.48, -3.16)

Nibble 45: (-2.89, 1.96)

Nibble 46: (-0.64, -1.16)

Nibble 47: (-0.76, 3.01)

Nibble 48: (-0.59, -3.60)

Nibble 49: (2.97, 3.11)

Nibble 50: (-1.17, -2.70)

Nibble 51: (-2.60, 1.00)

Nibble 52: (-0.86, -2.66)

Nibble 53: (0.64, -3.22)

Nibble 54: (-2.93, -3.24)

Nibble 55: (-2.71, 2.84)

Nibble 56: (-0.80, -3.20)

Nibble 57: (-3.31, -0.66)

Nibble 58: (-0.65, -1.10)

Nibble 59: (-1.10, 1.15)

Nibble 60: (-0.83, -0.95)

Nibble 61: (-2.79, -0.73)

Nibble 62: (-1.54, -1.14)

Nibble 63: (-1.07, 3.17)

Nibble 64: (-1.39, -3.29)

Nibble 65: (-3.15, 1.00)

Nibble 66: (-0.88, -1.42)

Nibble 67: (-3.11, -3.26)

Nibble 68: (-1.46, -3.17)

Nibble 69: (-0.93, 3.32)

Nibble 70: (-3.01, -3.32)

Nibble 71: (-3.30, 2.34)

Nibble 72: (-0.73, -1.35)

Nibble 73: (-3.00, 1.67)

Nibble 74: (-0.92, -2.98)

Nibble 75: (2.47, 1.22)

Nibble 76: (-0.93, -2.66)

Nibble 77: (-0.85, 0.93)

Nibble 78: (-2.65, -2.80)

Nibble 79: (-3.52, 2.65)

Nibble 80: (-1.37, -3.07)

Nibble 81: (2.72, 1.23)

Nibble 82: (-0.82, -2.74)

Nibble 83: (0.84, -3.04)

Nibble 84: (-3.36, -3.30)

Nibble 85: (-3.06, 2.64)

Nibble 86: (-1.31, 3.16)

Nibble 87: (-2.80, -3.27)

Nibble 88: (-0.61, -1.46)

Nibble 89: (-3.18, -3.04)

Nibble 90: (-0.72, -3.03)

Nibble 91: (2.77, 1.39)

Nibble 92: (-0.83, -0.75)

Nibble 93: (-1.09, 3.47)

Nibble 94: (-0.94, -3.23)

Nibble 95: (3.33, 1.28)

Nibble 96: (-0.72, -1.00)

Nibble 97: (-3.08, -1.07)

Nibble 98: (-1.23, -2.78)

Nibble 99: (2.84, 1.79)

Nibble 100: (-2.63, -3.08)

Nibble 101: (-3.35, 3.24)

Nibble 102: (-0.92, -3.32)

Nibble 103: (-2.86, -1.05)

Nibble 104: (-0.94, -0.54)

Nibble 105: (-0.95, 1.08)

Nibble 106: (-1.02, -2.96)

Nibble 107: (2.66, 1.44)

Nibble 108: (-1.38, -0.48)

Nibble 109: (-2.92, -1.05)

Nibble 110: (-0.78, -2.78)

Nibble 111: (2.52, 0.95)

Nibble 112: (-1.15, -3.21)

Nibble 113: (1.32, -3.06)

Nibble 114: (-1.04, -2.75)

Nibble 115: (-0.93, 1.52)

The program is actually read the content of txt files and decode a 16QAM signal to get the explained message,but it outputs the error message:

print(co + ” decodes as ” + point + ” – together, ” + bytes + ” gives “” + letter + “”n”)

TypeError: Can’t convert ‘bytes’ object to str implicitly

and unfortunately I can’t fix it.

## Expert Answer

The given code works fine on python 3.2 below versions. I have executed the code on python 2.7.

**OUTPUT GENERATED:**

(-0.78, 2.38) decodes as 0100

(-2.56, -3.35) decodes as 0010 – together, 01000010 gives “B”

(-0.90, -3.25) decodes as 0110

(3.28, 0.84) decodes as 1001 – together, 01101001 gives “i”

(-0.84, -3.08) decodes as 0110

(-3.10, -1.50) decodes as 0011 – together, 01100011 gives “c”

(-0.92, -2.87) decodes as 0110

(-1.17, 1.07) decodes as 0101 – together, 01100101 gives “e”

(-2.92, -3.14) decodes as 0010

(-2.97, 3.51) decodes as 0000 – together, 00100000 gives ” ”

(-0.91, -2.71) decodes as 0110

(3.19, 1.37) decodes as 1001 – together, 01101001 gives “i”

(-1.11, -0.97) decodes as 0111

(-3.10, -0.80) decodes as 0011 – together, 01110011 gives “s”

(-2.82, -2.83) decodes as 0010

(-2.55, 3.36) decodes as 0000 – together, 00100000 gives ” ”

(-0.80, -2.88) decodes as 0110

(0.89, 3.16) decodes as 1100 – together, 01101100 gives “l”

(-0.99, -3.51) decodes as 0110

(-1.22, 1.30) decodes as 0101 – together, 01100101 gives “e”

(-1.16, -0.86) decodes as 0111

(-3.19, -2.01) decodes as 0010 – together, 01110010 gives “r”

(-0.58, -1.42) decodes as 0111

(-2.91, -0.80) decodes as 0011 – together, 01110011 gives “s”

(-2.87, -2.76) decodes as 0010

(-2.72, 2.91) decodes as 0000 – together, 00100000 gives ” ”

(-1.31, -3.36) decodes as 0110

(-1.46, 0.84) decodes as 0101 – together, 01100101 gives “e”

(-1.09, -0.87) decodes as 0111

(3.53, 3.05) decodes as 1000 – together, 01111000 gives “x”

(-1.29, -0.99) decodes as 0111

(-2.91, 2.86) decodes as 0000 – together, 01110000 gives “p”

(-1.42, -2.77) decodes as 0110

(-0.41, 0.98) decodes as 0101 – together, 01100101 gives “e”

(-1.09, -2.74) decodes as 0110

(1.06, -2.88) decodes as 1110 – together, 01101110 gives “n”

(-1.21, -1.03) decodes as 0111

(-2.86, -1.03) decodes as 0011 – together, 01110011 gives “s”

(-1.19, -2.61) decodes as 0110

(2.97, 0.94) decodes as 1001 – together, 01101001 gives “i”

(-0.51, -1.20) decodes as 0111

(-1.00, -3.00) decodes as 0110 – together, 01110110 gives “v”

(-1.00, -3.57) decodes as 0110

(-1.30, 1.40) decodes as 0101 – together, 01100101 gives “e”

(-3.48, -3.16) decodes as 0010

(-2.89, 1.96) decodes as 0001 – together, 00100001 gives “!”

(-0.64, -1.16) decodes as 0111

(-0.76, 3.01) decodes as 0100 – together, 01110100 gives “t”

(-0.59, -3.60) decodes as 0110

(2.97, 3.11) decodes as 1000 – together, 01101000 gives “h”

(-1.17, -2.70) decodes as 0110

(-2.60, 1.00) decodes as 0001 – together, 01100001 gives “a”

(-0.86, -2.66) decodes as 0110

(0.64, -3.22) decodes as 1110 – together, 01101110 gives “n”

(-2.93, -3.24) decodes as 0010

(-2.71, 2.84) decodes as 0000 – together, 00100000 gives ” ”

(-0.80, -3.20) decodes as 0110

(-3.31, -0.66) decodes as 0011 – together, 01100011 gives “c”

(-0.65, -1.10) decodes as 0111

(-1.10, 1.15) decodes as 0101 – together, 01110101 gives “u”

(-0.83, -0.95) decodes as 0111

(-2.79, -0.73) decodes as 0011 – together, 01110011 gives “s”

(-1.54, -1.14) decodes as 0111

(-1.07, 3.17) decodes as 0100 – together, 01110100 gives “t”

(-1.39, -3.29) decodes as 0110

(-3.15, 1.00) decodes as 0001 – together, 01100001 gives “a”

(-0.88, -1.42) decodes as 0111

(-3.11, -3.26) decodes as 0010 – together, 01110010 gives “r”

(-1.46, -3.17) decodes as 0110

(-0.93, 3.32) decodes as 0100 – together, 01100100 gives “d”

(-3.01, -3.32) decodes as 0010

(-3.30, 2.34) decodes as 0000 – together, 00100000 gives ” ”

(-0.73, -1.35) decodes as 0111

(-3.00, 1.67) decodes as 0001 – together, 01110001 gives “q”

(-0.92, -2.98) decodes as 0110

(2.47, 1.22) decodes as 1001 – together, 01101001 gives “i”

(-0.93, -2.66) decodes as 0110

(-0.85, 0.93) decodes as 0101 – together, 01100101 gives “e”

(-2.65, -2.80) decodes as 0010

(-3.52, 2.65) decodes as 0000 – together, 00100000 gives ” ”

(-1.37, -3.07) decodes as 0110

(2.72, 1.23) decodes as 1001 – together, 01101001 gives “i”

(-0.82, -2.74) decodes as 0110

(0.84, -3.04) decodes as 1110 – together, 01101110 gives “n”

(-3.36, -3.30) decodes as 0010

(-3.06, 2.64) decodes as 0000 – together, 00100000 gives ” ”

(-1.31, 3.16) decodes as 0100

(-2.80, -3.27) decodes as 0010 – together, 01000010 gives “B”

(-0.61, -1.46) decodes as 0111

(-3.18, -3.04) decodes as 0010 – together, 01110010 gives “r”

(-0.72, -3.03) decodes as 0110

(2.77, 1.39) decodes as 1001 – together, 01101001 gives “i”

(-0.83, -0.75) decodes as 0111

(-1.09, 3.47) decodes as 0100 – together, 01110100 gives “t”

(-0.94, -3.23) decodes as 0110

(3.33, 1.28) decodes as 1001 – together, 01101001 gives “i”

(-0.72, -1.00) decodes as 0111

(-3.08, -1.07) decodes as 0011 – together, 01110011 gives “s”

(-1.23, -2.78) decodes as 0110

(2.84, 1.79) decodes as 1001 – together, 01101001 gives “i”

(-2.63, -3.08) decodes as 0010

(-3.35, 3.24) decodes as 0000 – together, 00100000 gives ” ”

(-0.92, -3.32) decodes as 0110

(-2.86, -1.05) decodes as 0011 – together, 01100011 gives “c”

(-0.94, -0.54) decodes as 0111

(-0.95, 1.08) decodes as 0101 – together, 01110101 gives “u”

(-1.02, -2.96) decodes as 0110

(2.66, 1.44) decodes as 1001 – together, 01101001 gives “i”

(-1.38, -0.48) decodes as 0111

(-2.92, -1.05) decodes as 0011 – together, 01110011 gives “s”

(-0.78, -2.78) decodes as 0110

(2.52, 0.95) decodes as 1001 – together, 01101001 gives “i”

(-1.15, -3.21) decodes as 0110

(1.32, -3.06) decodes as 1110 – together, 01101110 gives “n”

(-1.04, -2.75) decodes as 0110

(-0.93, 1.52) decodes as 0101 – together, 01100101 gives “e”

Decoded message with symbol errors included: Bice is lers expensive!than custard qie in Britisi cuisine

**Python 3. versions:**

In the new version the method to convert binary to ASCII is a bit different.

Changes have been made on line 55,56.

letter = binascii.unhexlify(‘%x’ %n) #line 55—-#Use this in case of python 2

letter = n.to_bytes((n.bit_length() + 7) // 8, ‘big’).decode() #line 56—–# for Python 3.2+

Line 56 is used in python 3. above versions

**CODE for python 3.2+ version:**

import binascii

file = open(“nibbles.txt”, “r”)

count = 1

bytes = “”

message = “”

for i, line in enumerate(file):

startIndex = line.index(‘(‘)

endIndex = line.index(‘)’)

co = line[startIndex:endIndex+1] # (-1.35, 2.30)

coordinates = line[startIndex+1:endIndex] # -1.35, 2.30

comma = coordinates.index(‘,’)

iVoltage = coordinates[0:comma]

iv = float(iVoltage)

qVoltage = coordinates[comma+2:]

qv = float(qVoltage)

if iv >= 0 and iv <= 2 and qv >= 0 and qv <= 2:

point = “1101”

if iv >= 2 and iv <= 4 and qv >= 0 and qv <= 2:

point = “1001”

if iv >= 0 and iv <= 2 and qv >= 2 and qv <= 4:

point = “1100”

if iv >= 2 and iv <= 4 and qv >= 2 and qv <= 4:

point = “1000”

if iv >= 0 and iv <= 2 and qv <= 0 and qv >= -2:

point = “1111”

if iv >= 2 and iv <= 4 and qv <= 0 and qv >= -2:

point = “1011”

if iv >= 0 and iv <= 2 and qv <= -2 and qv >= -4:

point = “1110”

if iv >= 2 and iv <= 4 and qv <= -2 and qv >= -4:

point = “1010”

if iv >= -4 and iv <= -2 and qv >= 0 and qv <= 2:

point = “0001”

if iv >= -2 and iv <= 0 and qv >= 0 and qv <= 2:

point = “0101”

if iv >= -4 and iv <= -2 and qv >= 2 and qv <= 4:

point = “0000”

if iv >= -2 and iv <= 0 and qv >= 2 and qv <= 4:

point = “0100”

if iv >= -4 and iv <= -2 and qv >= -2 and qv <= 0:

point = “0011”

if iv >= -2 and iv <= 0 and qv >= -2 and qv <= 0:

point = “0111”

if iv >= -4 and iv <= -2 and qv >= -4 and qv <= -2:

point = “0010”

if iv >= -2 and iv <= 0 and qv >= -4 and qv <= -2:

point = “0110”

bytes = bytes + str(point)

if count%2 == 0 :

n = int(bytes, 2)

#letter = binascii.unhexlify(‘%x’ %n)

letter = n.to_bytes((n.bit_length() + 7) // 8, ‘big’).decode() # for Python 3.2+

print(co + ” decodes as ” + point + ” – together, ” + bytes + ” gives “” + letter + “”n”)

bytes = “”

message = message + letter

else :

print(co + ” decodes as ” + bytes)

count += 1

print(“Decoded message with symbol errors included: ” + message)

For indentation purpose I am adding a **screenshot**