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

如何在python中將二進制字符串轉換為ascii字符串

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

問題描述

我制作了一個小 Python 程序,它從文件中讀取二進制文件并將其存儲到文本文件中,讀取文本文件并存儲二進制文件.但是,我無法讓二進制文件工作......它像這樣讀取文件:

I've made a little python program that reads binary from a file and stores it to a text file, read the text file and store the binary. But, I can't get the binary to work... it reads the files like this:

f_bin = open(bin_file,"rb")
to_bin_data = f_bin.read()
bin_data = bin(reduce(lambda x, y: 256*x+y, (ord(c) for c in to_bin_data), 0))
f_bin.close()

這個對我不起作用... 將二進制轉換為ASCII,反之亦然

類似這個網頁:http://www.roubaixinteractive.com/PlayGround/Binary_Conversion/Binary_To_Text.asp

我現在為它制作了一個很長的 if else 腳本,但感謝您的回答

推薦答案

我們來看看'hello'這個詞,它是0110100001100101011011000110110001101111

Let's take the word 'hello' which is 0110100001100101011011000110110001101111

要將其轉換回字符,我們可以使用 chrint(以 2 為基數)以及一些列表切片...

To translate that back to characters we can use chr and int (with a base of 2) and some list slicing...

''.join(chr(int(bin_text[i:i+8], 2)) for i in xrange(0, len(bin_text), 8))

如果我們想將 'hello' 轉換為二進制,我們可以使用 ord 和字符串格式化...

If we wanted to take 'hello' and convert it to binary we can use ord and string formatting...

''.join('{:08b}'.format(ord(c)) for c in 'hello')

這篇關于如何在python中將二進制字符串轉換為ascii字符串?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 国产一区二区高清在线 | 国产欧美久久精品 | 欧美日韩久久 | 精品91 | 岛国精品 | 亚洲精品视频一区二区三区 | 日本午夜在线视频 | 国产精品久久久久久久久久久免费看 | www.亚洲.com| 亚洲欧美在线观看视频 | 色婷婷av久久久久久久 | 久久久久久成人 | 久久久久成人精品亚洲国产 | 欧美综合网 | 国产成人精品在线播放 | 亚洲成av人影片在线观看 | 亚洲一区二区视频 | 99精品久久久国产一区二区三 | 九九热在线免费视频 | 欧美日韩专区 | 亚洲欧美一区二区三区国产精品 | 欧美一区二区三区电影 | 精品日韩一区二区 | 中文字幕一区二区三区精彩视频 | 九九精品网 | 日屁视频 | 亚洲欧美在线视频 | 国产精品久久久乱弄 | 欧美日韩亚洲二区 | 91最新入口 | 狠狠色综合欧美激情 | 曰韩三级 | 成人精品在线观看 | 欧产日产国产精品视频 | 国产日韩欧美二区 | 国产成人精品免高潮在线观看 | 99re在线视频| 国产一区二区三区视频 | 极品久久| 久久欧美精品 | 亚洲高清一区二区三区 |