數據轉換一直是廣泛使用的實用程序,其中之一可以是將二進製等效於其字符串的轉換。
讓我們討論一些可以做到這一點的方法。
方法#1:
天真的方法是通過取二進製數字的總和 (dn) 乘以它們的 2*(2^n) 的冪來將給定的二進製數據轉換為十進製。二進製數據被分成 7 位的集合,因為這組二進製作為輸入,返回對應的十進製值,即字符串字符的 ASCII 碼。然後使用 chr() 函數將此 ASCII 代碼轉換為字符串。
注意:這裏我們對 7 個集合中的二進製數據進行切片,因為原始 ASCII 表是在 7 位上編碼的,因此它有 128 個字符。
Python3
# Python3 code to demonstrate working of
# Converting binary to string
# Using BinarytoDecimal(binary)+chr()
# Defining BinarytoDecimal() function
def BinaryToDecimal(binary):
binary1 = binary
decimal, i, n = 0, 0, 0
while(binary != 0):
dec = binary % 10
decimal = decimal + dec * pow(2, i)
binary = binary//10
i += 1
return (decimal)
# Driver's code
# initializing binary data
bin_data ='10001111100101110010111010111110011'
# print binary data
print("The binary value is:", bin_data)
# initializing a empty string for
# storing the string data
str_data =' '
# slicing the input and converting it
# in decimal and then converting it in string
for i in range(0, len(bin_data), 7):
# slicing the bin_data from index range [0, 6]
# and storing it as integer in temp_data
temp_data = int(bin_data[i:i + 7])
# passing temp_data in BinarytoDecimal() function
# to get decimal value of corresponding temp_data
decimal_data = BinaryToDecimal(temp_data)
# Decoding the decimal value returned by
# BinarytoDecimal() function, using chr()
# function which return the string corresponding
# character for given ASCII value, and store it
# in str_data
str_data = str_data + chr(decimal_data)
# printing the result
print("The Binary value after string conversion is:",
str_data)
輸出:
The binary value is:10001111100101110010111010111110011 The Binary value after string conversion is: Geeks
方法#2:使用 int() 函數
每當為 int() 函數提供兩個參數時,它都會告訴 int() 函數第二個參數是輸入字符串的基數。如果輸入字符串大於 10,則 Python 假定下一個數字序列來自 ABCD……。因此,此概念可用於將二進製序列轉換為字符串。下麵是實現。
Python3
# Python3 code to demonstrate working of
# Converting binary to string
# Using BinarytoDecimal(binary)+chr()
# Defining BinarytoDecimal() function
def BinaryToDecimal(binary):
# Using int function to convert to
# string
string = int(binary, 2)
return string
# Driver's code
# initializing binary data
bin_data ='10001111100101110010111010111110011'
# print binary data
print("The binary value is:", bin_data)
# initializing a empty string for
# storing the string data
str_data =' '
# slicing the input and converting it
# in decimal and then converting it in string
for i in range(0, len(bin_data), 7):
# slicing the bin_data from index range [0, 6]
# and storing it in temp_data
temp_data = bin_data[i:i + 7]
# passing temp_data in BinarytoDecimal() function
# to get decimal value of corresponding temp_data
decimal_data = BinaryToDecimal(temp_data)
# Decoding the decimal value returned by
# BinarytoDecimal() function, using chr()
# function which return the string corresponding
# character for given ASCII value, and store it
# in str_data
str_data = str_data + chr(decimal_data)
# printing the result
print("The Binary value after string conversion is:",
str_data)
輸出:
The binary value is:10001111100101110010111010111110011 The Binary value after string conversion is: Geeks
相關用法
- Python String轉Binary用法及代碼示例
- Python image轉binary用法及代碼示例
- Python binary轉ASCII用法及代碼示例
- Python ASCII轉Binary用法及代碼示例
- Python Binary轉Hexadecimal用法及代碼示例
- Python floating轉binary用法及代碼示例
- Python Bytearray轉Hexadecimal String用法及代碼示例
- Python list轉string用法及代碼示例
注:本文由純淨天空篩選整理自__Shubham_Singh__大神的英文原創作品 Convert binary to string using Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。