問題描述
盡管有許多相關問題,但我找不到任何與我的問題相匹配的問題.我想將二進制字符串(例如,"0110100001101001"
)更改為字節數組(相同的示例,b"hi"
).
Despite the many related questions, I can't find any that match my problem. I'd like to change a binary string (for example, "0110100001101001"
) into a byte array (same example, b"hi"
).
我試過這個:
bytes([int(i) for i in "0110100001101001"])
但我得到了:
b'x00x01x01x00x01' #... and so on
在 Python 3 中執行此操作的正確方法是什么?
What's the correct way to do this in Python 3?
推薦答案
下面是 Patrick 提到的第一種方法的示例:將位串轉換為 int 并一次取 8 位.這樣做的自然方式以相反的順序生成字節.為了讓字節恢復到正確的順序,我在字節數組上使用擴展切片表示法,步長為 -1:b[::-1]
.
Here's an example of doing it the first way that Patrick mentioned: convert the bitstring to an int and take 8 bits at a time. The natural way to do that generates the bytes in reverse order. To get the bytes back into the proper order I use extended slice notation on the bytearray with a step of -1: b[::-1]
.
def bitstring_to_bytes(s):
v = int(s, 2)
b = bytearray()
while v:
b.append(v & 0xff)
v >>= 8
return bytes(b[::-1])
s = "0110100001101001"
print(bitstring_to_bytes(s))
顯然,Patrick 的第二種方式更為緊湊.:)
Clearly, Patrick's second way is more compact. :)
但是,在 Python 3 中有更好的方法來執行此操作:使用 int.to_bytes 方法:
However, there's a better way to do this in Python 3: use the int.to_bytes method:
def bitstring_to_bytes(s):
return int(s, 2).to_bytes((len(s) + 7) // 8, byteorder='big')
如果len(s)
保證是8的倍數,那么.to_bytes
的第一個arg可以簡化:
If len(s)
is guaranteed to be a multiple of 8, then the first arg of .to_bytes
can be simplified:
return int(s, 2).to_bytes(len(s) // 8, byteorder='big')
如果 len(s)
不是 8 的倍數,這將引發 OverflowError
,這在某些情況下可能是可取的.
This will raise OverflowError
if len(s)
is not a multiple of 8, which may be desirable in some circumstances.
另一種選擇是使用雙重否定來執行天花板除法.對于整數 a &b、樓層劃分使用//
Another option is to use double negation to perform ceiling division. For integers a & b, floor division using //
n = a // b
給出整數 n 使得
n <= a/b <n + 1
例如,47//10
給出 4,并且
gives the integer n such that
n <= a/b < n + 1
Eg,
47 // 10
gives 4, and
-47//10
給出 -5.所以
-(-47//10)
給出 5,有效地執行天花板除法.
-(-47 // 10)
gives 5, effectively performing ceiling division.
因此在 bitstring_to_bytes
我們可以 這樣做:
Thus in bitstring_to_bytes
we could do:
return int(s, 2).to_bytes(-(-len(s) // 8), byteorder='big')
然而,熟悉這種高效 & 的人并不多.緊湊的成語,因此通常認為它的可讀性不如
However, not many people are familiar with this efficient & compact idiom, so it's generally considered to be less readable than
return (s, 2).to_bytes((len(s) + 7) // 8, byteorder='big')
這篇關于在 Python 3 中將二進制字符串轉換為字節數組的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!