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

在 Python 中將字節(jié)的二進(jìn)制字符串表示形式轉(zhuǎn)換為

Convert binary string representation of a byte to actual binary value in Python(在 Python 中將字節(jié)的二進(jìn)制字符串表示形式轉(zhuǎn)換為實(shí)際的二進(jìn)制值)
本文介紹了在 Python 中將字節(jié)的二進(jìn)制字符串表示形式轉(zhuǎn)換為實(shí)際的二進(jìn)制值的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有一個(gè)字節(jié)的二進(jìn)制字符串表示,比如

I have a binary string representation of a byte, such as

01010101

如何將其轉(zhuǎn)換為真正的二進(jìn)制值并將其寫入二進(jìn)制文件?

How can I convert it to a real binary value and write it to a binary file?

推薦答案

使用int 函數(shù) 使用 base2 來讀取二進(jìn)制值作為整數(shù).

Use the int function with a base of 2 to read a binary value as an integer.

n = int('01010101', 2)

Python 2 使用字符串來處理二進(jìn)制數(shù)據(jù),因此您可以使用 chr() 函數(shù) 將整數(shù)轉(zhuǎn)換為單字節(jié)字符串.

Python 2 uses strings to handle binary data, so you would use the chr() function to convert the integer to a one-byte string.

data = chr(n)

Python 3 處理二進(jìn)制和文本的方式不同,因此您需要使用 <代碼>字節(jié)類型代替.這沒有直接等效于 chr() 函數(shù),但 bytes 構(gòu)造函數(shù)可以采用字節(jié)值列表.我們將 n 放在一個(gè)元素?cái)?shù)組中,并將其轉(zhuǎn)換為 bytes 對象.

Python 3 handles binary and text differently, so you need to use the bytes type instead. This doesn't have a direct equivalent to the chr() function, but the bytes constructor can take a list of byte values. We put n in a one element array and convert that to a bytes object.

data = bytes([n])

一旦你有了你的二進(jìn)制字符串,你就可以以二進(jìn)制模式打開一個(gè)文件并將數(shù)據(jù)寫入它,如下所示:

Once you have your binary string, you can open a file in binary mode and write the data to it like this:

with open('out.bin', 'wb') as f:
    f.write(data)

這篇關(guān)于在 Python 中將字節(jié)的二進(jìn)制字符串表示形式轉(zhuǎn)換為實(shí)際的二進(jìn)制值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個(gè)矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個(gè)矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 久久国际精品 | 日韩成人中文字幕 | 天堂一区二区三区 | 国产日韩欧美一区二区在线播放 | 91 在线| 国产激情视频在线 | 九九免费在线视频 | 中文字幕三区 | 日韩欧美电影在线 | 久久久精品国产 | av中文字幕在线播放 | 亚洲精品在线看 | 日韩在线观看中文字幕 | 久久影音先锋 | 日日干天天操 | 一区二区三区免费网站 | 成人欧美一区二区 | 亚洲婷婷一区 | 欧美黑人体内she精在线观看 | 欧美一级黄 | 亚洲激情综合 | 超碰在线97国产 | 国产日韩欧美综合 | 国产精品毛片 | 在线观看国产 | 黄色大片免费观看 | 欧美中文字幕一区 | 中日av | 爱高潮www亚洲精品 中文字幕免费视频 | 欧洲毛片 | 精品日韩一区 | 亚洲一一在线 | 免费看欧美一级片 | 亚洲日本视频 | 毛片一区二区 | 日日天天 | 精品啪啪 | 亚洲精品亚洲人成人网 | 日日操网站 | 人干人操 | 欧美区日韩区 |