简体   繁体   English

Python int 到二进制字符串?

[英]Python int to binary string?

Are there any canned Python methods to convert an Integer (or Long) into a binary string in Python?是否有任何罐装 Python 方法可以将 Integer(或 Long)转换为 Python 中的二进制字符串?

There are a myriad of dec2bin() functions out on Google... But I was hoping I could use a built-in function / library.谷歌上有无数的 dec2bin() 函数......但我希望我可以使用内置函数/库。

Python's string format method can take a format spec. Python 的字符串格式方法可以采用格式规范。

>>> "{0:b}".format(37)
'100101'

Format spec docs for Python 2格式化 Python 2 的规范文档

Format spec docs for Python 3格式化 Python 3 的规范文档

If you're looking for bin() as an equivalent to hex() , it was added in python 2.6.如果您正在寻找与hex()等效的bin() hex() ,它是在 python 2.6 中添加的。

Example:例子:

>>> bin(10)
'0b1010'

Python actually does have something already built in for this, the ability to do operations such as '{0:b}'.format(42) , which will give you the bit pattern (in a string) for 42 , or 101010 . Python的实际执行已经内置这一点,做业务的能力,例如一些'{0:b}'.format(42)它会给你的位模式(在一个字符串)为42 ,或101010


For a more general philosophy, no language or library will give its user base everything that they desire.对于更一般的哲学,没有任何语言或图书馆会为其用户群提供他们想要的一切 If you're working in an environment that doesn't provide exactly what you need, you should be collecting snippets of code as you develop to ensure you never have to write the same thing twice.如果您在一个不能完全提供您需要的环境中工作,您应该在开发时收集代码片段,以确保您永远不必两次编写相同的东西。 Such as, for example, the pseudo-code:例如,伪代码:

define intToBinString, receiving intVal:
    if intVal is equal to zero:
        return "0"
    set strVal to ""
    while intVal is greater than zero:
        if intVal is odd:
            prefix "1" to strVal
        else:
            prefix "0" to strVal
        divide intVal by two, rounding down
    return strVal

which will construct your binary string based on the decimal value.这将根据十进制值构造您的二进制字符串。 Just keep in mind that's a generic bit of pseudo-code which may not be the most efficient way of doing it though, with the iterations you seem to be proposing, it won't make much difference.请记住,这是一段通用的伪代码,虽然它可能不是有效的方法,但对于您似乎提出的迭代,它不会产生太大影响。 It's really just meant as a guideline on how it could be done.它实际上只是作为如何完成的指南。

The general idea is to use code from (in order of preference):总体思路是使用以下代码(按优先顺序):

  • the language or built-in libraries.语言或内置库。
  • third-party libraries with suitable licenses.具有合适许可证的第三方库。
  • your own collection.你自己的收藏。
  • something new you need to write (and save in your own collection for later).你需要写一些新的东西(并保存在你自己的收藏中以备后用)。

If you want a textual representation without the 0b-prefix, you could use this:如果你想要一个没有 0b 前缀的文本表示,你可以使用这个:

get_bin = lambda x: format(x, 'b')

print(get_bin(3))
>>> '11'

print(get_bin(-3))
>>> '-11'

When you want a n-bit representation:当您想要 n 位表示时:

get_bin = lambda x, n: format(x, 'b').zfill(n)
>>> get_bin(12, 32)
'00000000000000000000000000001100'
>>> get_bin(-12, 32)
'-00000000000000000000000000001100'

Alternatively, if you prefer having a function:或者,如果您更喜欢有一个功能:

def get_bin(x, n=0):
    """
    Get the binary representation of x.

    Parameters
    ----------
    x : int
    n : int
        Minimum number of digits. If x needs less digits in binary, the rest
        is filled with zeros.

    Returns
    -------
    str
    """
    return format(x, 'b').zfill(n)

As a reference:作为参考:

def toBinary(n):
    return ''.join(str(1 & int(n) >> i) for i in range(64)[::-1])

This function can convert a positive integer as large as 18446744073709551615 , represented as string '1111111111111111111111111111111111111111111111111111111111111111' .该函数可以转换一个大到18446744073709551615的正整数,表示为字符串'1111111111111111111111111111111111111111111111111111111111111111'

It can be modified to serve a much larger integer, though it may not be as handy as "{0:b}".format() or bin() .可以修改它以提供更大的整数,尽管它可能不如"{0:b}".format()bin()

This is for python 3 and it keeps the leading zeros !这是针对python 3的,它保留了前导零!

print(format(0, '08b'))

在此处输入图片说明

A simple way to do that is to use string format, see this page .一种简单的方法是使用字符串格式,请参阅此页面

>> "{0:b}".format(10)
'1010'

And if you want to have a fixed length of the binary string, you can use this:如果你想要一个固定长度的二进制字符串,你可以使用这个:

>> "{0:{fill}8b}".format(10, fill='0')
'00001010'

If two's complement is required, then the following line can be used:如果需要二进制补码,则可以使用以下行:

'{0:{fill}{width}b}'.format((x + 2**n) % 2**n, fill='0', width=n)

where n is the width of the binary string.其中 n 是二进制字符串的宽度。

one-liner with lambda :lambda 的单线

>>> binary = lambda n: '' if n==0 else binary(n/2) + str(n%2)

test:测试:

>>> binary(5)
'101'



EDIT :编辑

but then :(但是之后 :(

t1 = time()
for i in range(1000000):
     binary(i)
t2 = time()
print(t2 - t1)
# 6.57236599922

in compare to相比

t1 = time()
for i in range(1000000):
    '{0:b}'.format(i)
t2 = time()
print(t2 - t1)
# 0.68017411232

I am surprised there is no mention of a nice way to accomplish this using formatting strings that are supported in Python 3.6 and higher.我很惊讶没有提到使用 Python 3.6 及更高版本支持的格式化字符串来完成此操作的好方法。 TLDR:域名注册地址:

>>> number = 1
>>> f'0b{number:08b}'
'0b00000001'

Longer story更长的故事

This is functionality of formatting strings available from Python 3.6:这是 Python 3.6 提供的格式化字符串的功能:

>>> x, y, z = 1, 2, 3
>>> f'{x} {y} {2*z}'
'1 2 6'

You can request binary as well:您也可以请求二进制文件:

>>> f'{z:b}'
'11'

Specify the width:指定宽度:

>>> f'{z:8b}'
'      11'

Request zero padding:请求零填充:

f'{z:08b}'
'00000011'

And add common prefix to signify binary number:并添加公共前缀来表示二进制数:

>>> f'0b{z:08b}'
'0b00000011'

You can also let Python add the prefix for you but I do not like it so much as the version above because you have to take the prefix into width consideration:您也可以让 Python 为您添加前缀,但我不像上面的版本那么喜欢它,因为您必须考虑前缀的宽度:

>>> f'{z:#010b}'
'0b00000011'

More info is available in official documentation on Formatted string literals and Format Specification Mini-Language .更多信息可在Formatted string literalsFormat Specification Mini-Language 的官方文档中找到。

As the preceding answers mostly used format(), here is an f-string implementation.由于前面的答案主要使用 format(),这里是一个 f-string 实现。

integer = 7
bit_count = 5
print(f'{integer:0{bit_count}b}')

Output:输出:

00111 00111

For convenience here is the python docs link for formatted string literals: https://docs.python.org/3/reference/lexical_analysis.html#f-strings .为方便起见,这里是格式化字符串文字的 python 文档链接: https : //docs.python.org/3/reference/lexical_analysis.html#f-strings

Summary of alternatives:备选方案摘要:

n=42
assert  "-101010" == format(-n, 'b')
assert  "-101010" == "{0:b}".format(-n)
assert  "-101010" == (lambda x: x >= 0 and str(bin(x))[2:] or "-" + str(bin(x))[3:])(-n)
assert "0b101010" == bin(n)
assert   "101010" == bin(n)[2:]   # But this won't work for negative numbers.

Contributors include John Fouhy , Tung Nguyen , mVChr , Martin Thoma .贡献者包括John FouhyTung NguyenmVChrMartin Thoma and Martijn Pieters.和马丁·彼得斯。

>>> format(123, 'b')
'1111011'

For those of us who need to convert signed integers (range -2**(digits-1) to 2**(digits-1)-1) to 2's complement binary strings, this works:对于我们这些需要将有符号整数(范围 -2**(digits-1) 到 2**(digits-1)-1)转换为 2 的补码二进制字符串的人来说,这是有效的:

def int2bin(integer, digits):
    if integer >= 0:
        return bin(integer)[2:].zfill(digits)
    else:
        return bin(2**digits + integer)[2:]

This produces:这产生:

>>> int2bin(10, 8)
'00001010'
>>> int2bin(-10, 8)
'11110110'
>>> int2bin(-128, 8)
'10000000'
>>> int2bin(127, 8)
'01111111'

Using numpy pack/unpackbits, they are your best friends.使用 numpy pack/unpackbits,它们是你最好的朋友。

Examples
--------
>>> a = np.array([[2], [7], [23]], dtype=np.uint8)
>>> a
array([[ 2],
       [ 7],
       [23]], dtype=uint8)
>>> b = np.unpackbits(a, axis=1)
>>> b
array([[0, 0, 0, 0, 0, 0, 1, 0],
       [0, 0, 0, 0, 0, 1, 1, 1],
       [0, 0, 0, 1, 0, 1, 1, 1]], dtype=uint8)

you can do like that :你可以这样做:

bin(10)[2:]

or :或者 :

f = str(bin(10))
c = []
c.append("".join(map(int, f[2:])))
print c
def binary(decimal) :
    otherBase = ""
    while decimal != 0 :
        otherBase  =  str(decimal % 2) + otherBase
        decimal    //=  2
    return otherBase

print binary(10)

output:输出:

1010 1010

Yet another solution with another algorithm, by using bitwise operators.通过使用按位运算符,另一种算法的另一种解决方案。

def int2bin(val):
    res=''
    while val>0:
        res += str(val&1)
        val=val>>1     # val=val/2 
    return res[::-1]   # reverse the string

A faster version without reversing the string.无需反转字符串的更快版本。

def int2bin(val):
   res=''
   while val>0:
       res = chr((val&1) + 0x30) + res
       val=val>>1    
   return res 

The accepted answer didn't address negative numbers, which I'll cover.接受的答案没有解决负数,我将介绍。 In addition to the answers above, you can also just use the bin and hex functions.除了上面的答案,您还可以只使用binhex函数。 And in the opposite direction, use binary notation:在相反的方向,使用二进制表示法:

>>> bin(37)
'0b100101'
>>> 0b100101
37

But with negative numbers, things get a bit more complicated.但是对于负数,事情变得有点复杂。 The question doesn't specify how you want to handle negative numbers.这个问题没有指定你想如何处理负数。

Python just adds a negative sign so the result for -37 would be this: Python 只是添加了一个负号,所以 -37 的结果是这样的:

>>> bin(-37)
'-0b100101'

In computer/hardware binary data, negative signs don't exist.在计算机/硬件二进制数据中,不存在负号。 All we have is 1's and 0's.我们只有1和0。 So if you're reading or producing binary streams of data to be processed by other software/hardware, you need to first know the notation being used.因此,如果您正在读取或生成要由其他软件/硬件处理的二进制数据流,您首先需要知道所使用的符号。

One notation is sign-magnitude notation , where the first bit represents the negative sign, and the rest is the actual value.一种表示法是符号幅度表示法,其中第一位表示负号,其余表示实际值。 In that case, -37 would be 0b1100101 and 37 would be 0b0100101 .在这种情况下, -37 将是0b1100101 , 37 将是0b0100101 This looks like what python produces, but just add a 0 or 1 in front for positive / negative numbers.这看起来像 python 产生的,但只需在正/负数前面添加 0 或 1。

More common is Two's complement notation , which seems more complicated and the result is very different from python's string formatting.更常见的是Two'scomplement notation ,它看起来更复杂,结果与python的字符串格式有很大不同。 You can read the details in the link, but with an 8bit signed integer -37 would be 0b11011011 and 37 would be 0b00100101 .您可以阅读链接中的详细信息,但使用 8 位有符号整数 -37 将是0b11011011而 37 将是0b00100101

Python has no easy way to produce these binary representations. Python 没有简单的方法来生成这些二进制表示。 You can use numpy to turn Two's complement binary values into python integers:您可以使用numpy将二进制补码二进制值转换为 python 整数:

>>> import numpy as np
>>> np.int8(0b11011011)
-37
>>> np.uint8(0b11011011)
219
>>> np.uint8(0b00100101)
37
>>> np.int8(0b00100101)
37

But I don't know an easy way to do the opposite with builtin functions.但是我不知道用内置函数做相反的事情的简单方法。 The bitstring package can help though.不过,位串包可以提供帮助。

>>> from bitstring import BitArray
>>> arr = BitArray(int=-37, length=8)
>>> arr.uint
219
>>> arr.int
-37
>>> arr.bin
'11011011'
>>> BitArray(bin='11011011').int
-37
>>> BitArray(bin='11011011').uint
219

Python 3.6 added a new string formatting approach called formatted string literals or “f-strings”. Python 3.6 添加了一种新的字符串格式化方法,称为格式化字符串文字或“f-strings”。 Example:例子:

name = 'Bob'
number = 42
f"Hello, {name}, your number is {number:>08b}"

Output will be 'Hello, Bob, your number is 00001010!'输出将是“你好,鲍勃,你的号码是 00001010!”

A discussion of this question can be found here - Here关于这个问题的讨论可以在这里找到 - 这里

除非我误解了二进制字符串的意思,否则我认为您要查找的模块是struct

Here is the code I've just implemented.这是我刚刚实现的代码。 This is not a method but you can use it as a ready-to-use function !这不是一种方法,但您可以将其用作现成的功能

def inttobinary(number):
  if number == 0:
    return str(0)
  result =""
  while (number != 0):
      remainder = number%2
      number = number/2
      result += str(remainder)
  return result[::-1] # to invert the string
n=input()
print(bin(n).replace("0b", ""))

numpy.binary_repr(num, width=None)

Examples from the documentation link above:上面文档链接中的示例:

 >>> np.binary_repr(3) '11' >>> np.binary_repr(-3) '-11' >>> np.binary_repr(3, width=4) '0011'

The two's complement is returned when the input number is negative and width is specified:当输入数字为负且指定宽度时,返回二进制补码:

 >>> np.binary_repr(-3, width=3) '101' >>> np.binary_repr(-3, width=5) '11101'

Somewhat similar solution有点类似的解决方案

def to_bin(dec):
    flag = True
    bin_str = ''
    while flag:
        remainder = dec % 2
        quotient = dec / 2
        if quotient == 0:
            flag = False
        bin_str += str(remainder)
        dec = quotient
    bin_str = bin_str[::-1] # reverse the string
    return bin_str 

here is simple solution using the divmod() fucntion which returns the reminder and the result of a division without the fraction.这是使用 divmod() 函数的简单解决方案,它返回提醒和不带分数的除法结果。

def dectobin(number):
    bin = ''
    while (number >= 1):
        number, rem = divmod(number, 2)
        bin = bin + str(rem)
    return bin

Here's yet another way using regular math, no loops, only recursion.这是使用常规数学的另一种方法,没有循环,只有递归。 (Trivial case 0 returns nothing). (普通情况 0 不返回任何内容)。

def toBin(num):
  if num == 0:
    return ""
  return toBin(num//2) + str(num%2)

print ([(toBin(i)) for i in range(10)])

['', '1', '10', '11', '100', '101', '110', '111', '1000', '1001']

Calculator with all neccessary functions for DEC,BIN,HEX: (made and tested with Python 3.5)带有 DEC、BIN、HEX 的所有必要函数的计算器:(使用 Python 3.5 制作和测试)

You can change the input test numbers and get the converted ones.您可以更改输入测试编号并获取转换后的测试编号。

# CONVERTER: DEC / BIN / HEX

def dec2bin(d):
    # dec -> bin
    b = bin(d)
    return b

def dec2hex(d):
    # dec -> hex
    h = hex(d)
    return h

def bin2dec(b):
    # bin -> dec
    bin_numb="{0:b}".format(b)
    d = eval(bin_numb)
    return d,bin_numb

def bin2hex(b):
    # bin -> hex
    h = hex(b)
    return h

def hex2dec(h):
    # hex -> dec
    d = int(h)
    return d

def hex2bin(h):
    # hex -> bin
    b = bin(h)
    return b


## TESTING NUMBERS
numb_dec = 99
numb_bin = 0b0111 
numb_hex = 0xFF


## CALCULATIONS
res_dec2bin = dec2bin(numb_dec)
res_dec2hex = dec2hex(numb_dec)

res_bin2dec,bin_numb = bin2dec(numb_bin)
res_bin2hex = bin2hex(numb_bin)

res_hex2dec = hex2dec(numb_hex)
res_hex2bin = hex2bin(numb_hex)



## PRINTING
print('------- DECIMAL to BIN / HEX -------\n')
print('decimal:',numb_dec,'\nbin:    ',res_dec2bin,'\nhex:    ',res_dec2hex,'\n')

print('------- BINARY to DEC / HEX -------\n')
print('binary: ',bin_numb,'\ndec:    ',numb_bin,'\nhex:    ',res_bin2hex,'\n')

print('----- HEXADECIMAL to BIN / HEX -----\n')
print('hexadec:',hex(numb_hex),'\nbin:    ',res_hex2bin,'\ndec:    ',res_hex2dec,'\n')

To calculate binary of numbers:计算二进制数:

print("Binary is {0:>08b}".format(16))

To calculate the Hexa decimal of a number :要计算一个数字的十六进制小数

print("Hexa Decimal is {0:>0x}".format(15))

To Calculate all the binary no till 16::计算所有二进制直到 16 ::

for i in range(17):
   print("{0:>2}: binary is {0:>08b}".format(i))

To calculate Hexa decimal no till 17计算十六进制小数到 17

 for i in range(17):
    print("{0:>2}: Hexa Decimal is {0:>0x}".format(i))
##as 2 digit is enogh for hexa decimal representation of a number

If you are willing to give up "pure" Python but gain a lot of firepower, there is Sage - example here : 如果你愿意放弃“纯粹的”Python而获得大量的火力, 那么就有Sage - 这里的例子如下:

sage: a = 15
sage: a.binary()
'1111'

You'll note that it returns as a string, so to use it as a number you'd want to do something like 您会注意到它以字符串形式返回,因此要将其用作您想要执行某些操作的数字

sage: eval('0b'+b)
15
try:
    while True:
        p = ""
        a = input()
        while a != 0:
            l = a % 2
            b = a - l
            a = b / 2
            p = str(l) + p
        print(p)
except:
    print ("write 1 number")

I found a method using matrix operation to convert decimal to binary.我找到了一种使用矩阵运算将十进制转换为二进制的方法。

import numpy as np
E_mat = np.tile(E,[1,M])
M_order = pow(2,(M-1-np.array(range(M)))).T
bindata = np.remainder(np.floor(E_mat /M_order).astype(np.int),2)

E is input decimal data, M is the binary orders. E是输入的十进制数据, M是二进制顺序。 bindata is output binary data, which is in a format of 1 by M binary matrix. bindata为输出二进制数据,格式为1×M二进制矩阵。

Along a similar line to Yusuf Yazici's answer 沿着与Yusuf Yazici的答案类似的路线

def intToBin(n):
    if(n < 0):
        print "Sorry, invalid input."
    elif(n == 0):
        print n
    else:
        result = ""
        while(n != 0):
            result += str(n%2)
            n /= 2
        print result[::-1]

I adjusted it so that the only variable being mutated is result (and n of course). 我对它进行了调整,以便变异的唯一变量是结果(当然是n)。

If you need to use this function elsewhere (ie, have the result used by another module), consider the following adjustment: 如果您需要在其他地方使用此功能(即,将结果用于其他模块),请考虑以下调整:

def intToBin(n):
    if(n < 0):
        return -1
    elif(n == 0):
        return str(n)
    else:
        result = ""
        while(n != 0):
            result += str(n%2)
            n /= 2
        return result[::-1]

So -1 will be your sentinel value indicating the conversion failed. 因此-1将是您的哨兵值,表明转换失败。 (This is assuming you are converting ONLY positive numbers, whether they be integers or longs). (假设您只转换正数,无论它们是整数还是长数)。

Here's a simple binary to decimal converter that continuously loops 这是一个简单的二进制到十进制转换器,它不断循环

t = 1
while t > 0:
    binaryNumber = input("Enter a binary No.")
    convertedNumber = int(binaryNumber, 2)

    print(convertedNumber)

print("")

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

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