Python int too large to convert to C int, binary to ASCII

Viewed 248

I can't decode this message: 01010010010001010101001101010101010001010100110001010100010011110010110101010100010000010101001001000101010000010011001000101101010011000100111101000111010010010100001101000001001011010100001000110010001100000011001000110000

I'd try with this, but I can't solve it. It says "Python int too large to convert to C int"

 binaryString = input("Code: ")

bValues = binaryString.split(" ")
string = ""
for bValue in bValues:
    integer = int(bValue, 2)
    character = chr(integer)
    string += character

print ("The message is: ")
print ("")
print(string)
print ("")
print ("Thanks!")
a1 = input("Press enter to exit.")
if a1 in ("x"):
    exit()
2 Answers

The input is one long string, you need to split it to groups of 8 characters instead of splitting by white space, which will create a list of size 1 with the entire input

binaryString = input("Code: ").strip()

size = 8
bValues = [binaryString[i:i+size] for i in range(0, len(binaryString), size)]
string = ""
for bValue in bValues:
    integer = int(bValue, 2)
    character = chr(integer)
    string += character

Output

The message is: 

RESUELTO-TAREA2-LOGICA-B2020

Thanks!

You do a .split(" ") which suggests that you expect there to be spaces separating secionts (presumably bytes) of your input - however there isn't any in your input. As such, when you convert it to an integer you get 8664126781545987092300862719515900143572349217612874568383738163760, which most definitely won't fit in C integer storage, and so when you try and do the chr() conversion you get an error.

To make your code work, I suggest either adding the spaces to separate the chunks of the code, or iterating over sections of your input that correspond with your desired length.

Related