python-ValueError:以2为基数的int()无效。

时间:2021-08-17 22:32:45

I am getting error in following lines. error is not recurring but only sometimes

我在以下几行中出错。错误不是反复出现的,而是偶尔出现的。

x,y are huge numbers of 2048 bits
z=bin(x)+bin(y)
z=int(z,2)

ValueError: invalid literal for int() with base 2: '10010101101001011011000000001111001110111110000100101000000011111111100000111010111011101111110010001101101001101000100000001100010011000010100000110100100001010110011111101101000101101001011001100110'

4 个解决方案

#1


4  

Are you sure you haven't faked that error message?

你确定你没有伪造那个错误信息吗?

The code...

代码…

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

...works for me.

…为我工作。

And, a concrete example of your 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 for int() with base 2: '0b1001110100010001111000010000b1011001011000111100011000'

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

…在错误消息中显示问题(即0b前缀)。

The solution would be to either strip the prefixes with...

解决方案是将前缀与…

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

...or, as Martijn Pieters suggests, generate the binary representation without prefixes using format()...

…或者,正如Martijn Pieters所建议的,使用format()来生成无前缀的二进制表示…

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

...or, as gnibbler suggests, use the string object's format() method to do it in one call...

…或者,正如gnibbler所建议的,使用string对象的format()方法在一个调用中执行它……

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

#2


0  

bin will return string in format:

bin将返回字符串格式:

'0b1100000011001011101000111010110011'

With first '0b'

与第一“0 b”

So, for you code, you can use this (will sum x and y as integers):

因此,对于你的代码,你可以使用这个(将x和y作为整数):

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

Or, if you want exactly first to concatinate the x and y as strings:

或者,如果你想先把x和y合并成字符串:

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

#3


0  

bin gives you a string representation so bin( .. ) + bin( .. ) concats two string, which is not a valid result.

bin给你一个字符串表示,所以bin(..)+本(. .)concat两个字符串,这不是一个有效的结果。

>>> bin(0) + bin(1)
'0b00b1'

In case you are trying to work with actual binary data (not string representations of integer representation of binary data, which is what your code does) then you should use the struct module instead.

如果您试图使用实际的二进制数据(而不是二进制数据的整数表示形式的字符串表示,这就是代码所做的),那么您应该使用struct模块。

#4


0  

Actually the inbuild sqrt function was not working for my python 2.7.2 Ubuntu 12.04. So I used user defined function for square root. thanks everyone for help.

实际上,inbuild sqrt函数并不适用于我的python 2.7.2 Ubuntu 12.04。所以我用了用户定义的平方根函数。谢谢大家帮忙。

def ceilofsqrt(N):
    answerlower = 1
    answerupper = N
    while answerupper - answerlower  > 2:
        answerupper = (answerupper + answerlower)/2 + (answerupper + answerlower)%2
        answerlower = N/answerupper
    guess = answerlower
    while True:    
        if guess**2 >= N:
            return guess
        guess += 1

#1


4  

Are you sure you haven't faked that error message?

你确定你没有伪造那个错误信息吗?

The code...

代码…

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

...works for me.

…为我工作。

And, a concrete example of your 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 for int() with base 2: '0b1001110100010001111000010000b1011001011000111100011000'

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

…在错误消息中显示问题(即0b前缀)。

The solution would be to either strip the prefixes with...

解决方案是将前缀与…

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

...or, as Martijn Pieters suggests, generate the binary representation without prefixes using format()...

…或者,正如Martijn Pieters所建议的,使用format()来生成无前缀的二进制表示…

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

...or, as gnibbler suggests, use the string object's format() method to do it in one call...

…或者,正如gnibbler所建议的,使用string对象的format()方法在一个调用中执行它……

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

#2


0  

bin will return string in format:

bin将返回字符串格式:

'0b1100000011001011101000111010110011'

With first '0b'

与第一“0 b”

So, for you code, you can use this (will sum x and y as integers):

因此,对于你的代码,你可以使用这个(将x和y作为整数):

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

Or, if you want exactly first to concatinate the x and y as strings:

或者,如果你想先把x和y合并成字符串:

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

#3


0  

bin gives you a string representation so bin( .. ) + bin( .. ) concats two string, which is not a valid result.

bin给你一个字符串表示,所以bin(..)+本(. .)concat两个字符串,这不是一个有效的结果。

>>> bin(0) + bin(1)
'0b00b1'

In case you are trying to work with actual binary data (not string representations of integer representation of binary data, which is what your code does) then you should use the struct module instead.

如果您试图使用实际的二进制数据(而不是二进制数据的整数表示形式的字符串表示,这就是代码所做的),那么您应该使用struct模块。

#4


0  

Actually the inbuild sqrt function was not working for my python 2.7.2 Ubuntu 12.04. So I used user defined function for square root. thanks everyone for help.

实际上,inbuild sqrt函数并不适用于我的python 2.7.2 Ubuntu 12.04。所以我用了用户定义的平方根函数。谢谢大家帮忙。

def ceilofsqrt(N):
    answerlower = 1
    answerupper = N
    while answerupper - answerlower  > 2:
        answerupper = (answerupper + answerlower)/2 + (answerupper + answerlower)%2
        answerlower = N/answerupper
    guess = answerlower
    while True:    
        if guess**2 >= N:
            return guess
        guess += 1