简体   繁体   English

将二进制字符串转换为整数(Python)

[英]Convert Binary String into Integer (Python)

I have made the following code. 我做了以下代码。 The idea is that this binStrToInt actually recognizes my string as an integer, and can turn it into the actual number. 想法是该binStrToInt实际上将我的字符串识别为整数,并将其转换为实际数字。 My peers have pointed out that my code has a few errors. 同行指出,我的代码有一些错误。 The only ones that I have noticed is that 1. math needs to be imported (for int) 2. return needs to be indented 3. num = num + int(binary_Str[i]) the i shouldn't be in that line? 我注意到的唯一一个问题是:1.需要导入数学(用于int)2.返回必须缩进3. num = num + int(binary_Str [i])我不应该在那一行吗? The issue: it just doesn't work. 问题:它根本不起作用。 I have no idea what to do 我不知道该怎么做

def binStrToInt(binary_str):


    """The function binStrToInt() takes in one input, a string of ones and 
    zeros (no spaces) called BINARY_STR.  It treats that input as a binary 
    number (base 2) and converts it to a decimal integer (base 10). It 
    returns an integer result."""

    length = len(binary_str)

    num = 0
    for i in range(length)
        num = num + int(binary_Str[i])
        num = num * 2
return num / 2

It works fine when you remove all the typos and indentation problems: 当您消除所有错字和缩进问题时,它可以正常工作:

def binStrToInt(binary_str):


    """The function binStrToInt() takes in one input, a string of ones and
    zeros (no spaces) called BINARY_STR.  It treats that input as a binary
    number (base 2) and converts it to a decimal integer (base 10). It
    returns an integer result."""

    length = len(binary_str)

    num = 0
    for i in range(length):
        num = num + int(binary_str[i])
        num = num * 2
    return num / 2

But honestly, how did you not see these? 但老实说,您怎么看不到这些? The interpreter tells you about most of them (all except the return indentation). 解释器会告诉您大多数信息(除了返回缩进)。 And as commenters have mentioned, the built-in int() function will do the same thing if you pass it a string, and the number 2 to cause it to convert from a binary string. 正如评论者所提到的那样,如果将字符串传递给数字,并且将数字2转换为二进制字符串,则内置的int()函数将执行相同的操作。

EDIT Based on your comment below, you are somehow using a special quote, perhaps copied from a web-page or something. 编辑根据下面的评论,您以某种方式使用了特殊的报价,也许是从网页或其他内容中复制的。 That won't work -- here is an example of something that worked and something that didn't in the interpreter: 那是行不通的-这是解释器中不起作用的示例:

>>> def binStrToInt(binary_str):
...     length = len(binary_str)
...     num = 0
...     for i in range(length):
...         num = num + int(binary_str[i])
...         num = num * 2
...     return num / 2
... 
>>> binStrToInt('101')
5
>>> binStrToInt(’101’)
  File "<stdin>", line 1
    binStrToInt(’101’)
                ^
SyntaxError: invalid syntax

You could make the logic a bit more easy to understand if you can re-write your code as 如果您可以重新编写代码,则可以使逻辑更容易理解

num = 0
for i in range(len(binary_str)):
    num = num * 2
    num = num + int(binary_str[i])
return num

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM