数据转换一直是广泛使用的实用程序,其中之一可以是将二进制等效于其字符串的转换。
让我们讨论一些可以做到这一点的方法。
方法#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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。