Question & Answer: here's the program:…..

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

 

You just have to convert bytes object letter to str explicitly, that’s all

correctedScript.py

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 “” + str(letter) + “”n”) # Corrected code
bytes = “”
message = message + str(letter) # Corrected code
else :
print(co + ” decodes as ” + bytes)

count += 1

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

Still stressed from student homework?
Get quality assistance from academic writers!