runtime error - python-ValueError: invalid literal for int() with base 2 -


i getting error in following lines. error not recurring

x,y huge numbers of 2048 bits z=bin(x)+bin(y) z=int(z,2)  valueerror: invalid literal int() base 2: '10010101101001011011000000001111001110111110000100101000000011111111100000111010111011101111110010001101101001101000100000001100010011000010100000110100100001010110011111101101000101101001011001100110' 

are sure haven't faked error message?

the code...

>>> int('10010101101001011011000000001111001110111110000100101000000011111111100000111010111011101111110010001101101001101000100000001100010011000010100000110100100001010110011111101101000101101001011001100110', 2) 939350809951131205472627037306557272273273866819979105965670l 

...works me.

and, concrete example of code...

>>> x = 82349832 >>> y = 23432984 >>> z = bin(x) + bin(y) >>> int(z, 2) traceback (most recent call last):   file "<stdin>", line 1, in <module> valueerror: invalid literal int() base 2: '0b1001110100010001111000010000b1011001011000111100011000' 

...shows problem (i.e. 0b prefixes) in error message.

the solution either strip prefixes with...

z = bin(x)[2:] + bin(y)[2:] z = int(z, 2) 

...or, martijn pieters suggests, generate binary representation without prefixes using format()...

z = format(x, 'b') + format(y, 'b') z = int(z, 2) 

...or, gnibbler suggests, use string object's format() method in 1 call...

z = '{:b}{:b}'.format(x, y) z = int(z, 2) 

Comments