久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Python 3 中將二進制字符串轉換為字節數組

Convert binary string to bytearray in Python 3(在 Python 3 中將二進制字符串轉換為字節數組)
本文介紹了在 Python 3 中將二進制字符串轉換為字節數組的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

盡管有許多相關問題,但我找不到任何與我的問題相匹配的問題.我想將二進制字符串(例如,"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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 一区中文 | 免费观看国产视频在线 | 成人av高清 | av在线免费观看网站 | 四虎永久免费影院 | 国内精品久久久久 | 日韩在线一区二区三区 | 午夜精品久久久久久久星辰影院 | 国产精品乱码一二三区的特点 | 国产精品久久久久久久三级 | 天天综合网天天综合色 | 国产精品免费福利 | 国产特级毛片 | 91免费在线 | 一级毛片视频在线 | 日韩中文字幕视频 | 日韩久久久久 | 99视频在线免费观看 | 亚洲欧美日韩精品久久亚洲区 | 国产一级视频在线 | 97avcc| 久久69精品久久久久久久电影好 | 日韩国产中文字幕 | 国产精品免费大片 | 国产精品一区久久久 | 日韩1区 | 超碰伊人| 精品一区二区三区四区五区 | 国产精品7777777 | 国产精品久久久久无码av | www.久草.com| 国产一级片在线播放 | www.色综合| 国产精品久久久久久久久久免费看 | 激情五月激情综合网 | 天天看天天操 | 国产精品成人一区二区 | 亚洲导航深夜福利涩涩屋 | 蜜桃av鲁一鲁一鲁一鲁 | 狠狠色狠狠色综合日日92 | av在线免费播放 |