# 4 ways to Convert a Binary String to a Normal String

A Binary String is used to store data in the form of bytes. A byte is a unit in computer programming that is made up of 8 bits. The byte is used to store many data formats such as audio files, video files, and images.

A Binary String is only made up of 0’s and 1’s. It is a sequence of zeros and ones that have a specific meaning associated with them.

A normal string also called a character string can include all the numbers, letters, and symbols in a sequence. The sequence can have a meaning which can be understood by humans.

While binary data is purely designed to be interpreted by computers.

The binary strings are used to perform low-level operations like bitwise operations, and the character strings are used to perform high-level processing tasks such as text processing and even natural language processing.

Related: Read a similar post on how to convert bytes to Unicode.

The character strings need to have an encoding format specified to be able to interchange in between different formats.

In this post, we are going to focus on how to create a binary string and also learn the different possibilities of converting binary strings into normal strings.

## What Is a Binary String?

As discussed above, a binary string stores information in the form of zeros and ones since computers do not understand the normal language we speak or write. So there are many approaches to converting human language into a low-level language that the machine understands.

There are a few ways to create a binary string in Python.

Before we jump to examples, we need to be clear about something. In Python, whenever we try to represent a binary string, there is always a prefix called the `0b` prefix used to represent that it is a binary number.

Most of the time, when we try to convert the binary string to a character string or vice versa, we try to eliminate this prefix.

### Using the bin() Function

The bin() function is a built-in Python function used to obtain the binary representation of an integer.

```num=74
print("The number is:",num)
x=bin(num)
print("The binary representation of ",num, "is:",x)
```

In this code, we are assigning the value 74 to the variable num. We need to represent 74 in binary format. In the next line, we are printing the number.

Next, we are using the bin function and pass the `num` variable as an argument. The result is stored in x.

The binary representation is printed in the next line.

As discussed above, the `0b` prefix is included before the binary string. There is a way to remove the prefix.

```num=74
print("The number is:",num)
x=bin(num)[2:]
print("The binary representation of ",num, "is:",x)
print(type(num))
print(type(x))
```

This code is the same as the above code. But, we used the slicing operator(:) in line 3 to remove the `0b` prefix. Since indexing starts from zero in Python, 0 and b have the positions 0 and 1 respectively. So we are trying to print the contents of the x variable starting from the second position.

The last two lines also print the type of num and x respectively.

### Converting a Character String to a Binary String

We have seen how to convert an integer to a binary string. Now let us take a character string and convert it to a binary string.

```mes = "AskPython"
bstr = ' '.join(format(ord(c), '08b') for c in mes)
print(bstr)
print(type(bstr))
```

The string that we are trying to convert to a binary string is “AskPython” which is assigned to the variable mes.

In the next line, we have created a variable called bstr which is used to convert the character string to a binary string.

Let us break this line of code.

` ' '` – The binary representation is supposed to be printed with a space in between them so we use quotes with a space.

`.join(format(ord(c), '08b') for c in mess` – The join function is used to concatenate the different binary strings obtained by the ord function that is used to get the Unicode value of each character in the string. The values obtained by the ord function are converted to the binary representation with the help of the format specifier 08b such that each part of the binary representation has only eight bits. There is also a for loop iterating to convert each character in the string to binary.

Next, we are printing the binary string. In the last line, we are also printing the type of the new binary string.

## Convert Binary String to Normal String

Let us try to convert a binary string to a normal string using some approaches.

### Using Bitarray to Convert Binary String to Normal String

The bitarray is a library of the PyPI project used to produce an array of booleans(0 and 1). Since it is a library, it needs to be installed before using it.

Here is how you can install the library

```Using the pip command
pip install bitarray
In the conda terminal
conda install bitarray
In notebooks(Jupyter or colab)
! pip install bitarray
```

Once it is installed, we can import it and use it.

```from bitarray import bitarray
bins = "01000001 01110011 01101011 01010000 01111001 01110100 01101000 01101111 01101110"
bts = bitarray(bins)
ascs = bts.tobytes().decode('ascii')
print("The normal string is: ", ascs)
print(type(ascs))
```

Firstly, we are importing the bitarray method from the bitarray library.

Next, a variable called bins is used to store some binary data. Another variable called bts is created to generate an 8-bit sequence of booleans from the binary data.

A variable called ascs is used to store the conversion of the binary data to normal string with the help of tobytes and decode. The encoding schema is specified to be ascii.

The next two lines print the normal string and the type of this normal string.

### Using a for Loop to Convert Binary String to Normal String

The core concept of converting the binary string to a normal string remains the same. But we can try using a for loop to iterate across the binary data and generate a normal string.

```bins = "0100100001100101011011000110110001101111"
str = ""
for i in range(0, len(bins), 8):
binc = bins[i:i + 8]
num = int(binc, 2)
str += chr(num)
print("The normal representation of ",bins ,"is", str)
print(type(str))
```

First of all we declare a byte data and assign it to bins. An empty string str is created to store the normal string after conversion.

We initialize a for loop to run across the binary data starting from 0, through the length of the bins with an incrementor 8. We did this because each binary data is made up of 8 bits.

binc stands for binary chunk, which is a part of the binary data each having 8 bits.

Now we have binary data divided into equal chunks, we now need to convert these chunks into decimal values with the help of the `int()` method along with a base 2.

The decimal value is converted to the associated ASCII value with the help of `chr()`. This ASCII value is appended to the empty string str.

Lastly, we print the normal string stored in str and also the type of the str variable.

### Using List Comprehension

List Comprehension is just a short form to lessen the code which is spread across multiple lines.

Read: What is list comprehension in Python?

The code is given below.

```bins="01001000011001010110110001101100011011110101011101101111011100100110110001100100"
binc = [bins[i:i + 8] for i in range(0, len(bins), 8)]
nums = [int(chunk, 2) for chunk in binc]
str1 = ''.join(chr(num) for num in nums)
print("The normal string is:", str1)
```

As usual, we are assigning binary data to the variable bins. Next, we are trying to create uniform lengthed chunks of binary data called binc.

Now we have binary data divided into equal chunks, we now need to convert these chunks into decimal values with the help of the `int()` method along with a base 2.

A variable called str1 is used to store the normal string after conversion.

Lastly, we are printing the normal string.

Observe the previous example and this one. Both are essentially the same. But with list comprehension, we can shorten the code and make it simple to read.

### Using Bitwise Operators

We can use bitwise operations to divide the huge binary data into uniform chunks. Let us see how we can do that.

```bins="0100100001100101011011000110110001101111001000000101011101101111011100100110110001100100"
num = int(bins, 2)
str1 = num.to_bytes((num.bit_length() + 7) // 8, 'big').decode('ascii')
print("The normal string is :", str1)
type(str1)
```

Like always, we assigned random binary data to a variable called bins.

Next, we are creating a variable called num to store the decimal values corresponding to the binary data in a base 2 integer format.

A variable called str1 is created to store the result after conversion. To explain more clearly, the total length of the num variable is added with 7 and is then divided by 8 to obtain equal chunks of binary data. This decimal data is converted to bytes and then decoded. `big` is used to specify the most significant bit to be transferred first.

The string and the type of output are printed in the last two lines.

## Conclusion

To wrap it up, we have learned what is the binary representation scheme and how it is easy for computers to understand since it represents data in the form of zeros and ones.

We have also seen the differences between a character string and a binary string. While a binary string is used to perform low-level operations, a character string is used to perform high-level processing tasks such as NLP.

Next, we learned how to create a binary string from an integer using the bin() function. We have also seen how the binary data is represented with a 0b prefix and how to remove this prefix while creating a binary string.

Coming to the reverse conversion(binary string to normal string), we have learned four approaches.

In the first, we used a third party library called `bitarray` used to deal with huge binary data. This binary data is converted into bytes which are then converted to normal ASCII string.

In the next approach, we used a for loop to perform the same operation. But, we divided the huge binary data into chunks of equal sizes. These chunks are converted into integer values with a base 2 encoding. These integer values are used to decode into a normal string with the decoding format -ascii.

The next approach is list comprehension. which is the modified form of the previous example.

The last approach is using bitwise operator(//) to divide the binary data into uniform-sized chunks.

## References

Visit the official documentation of PyPI to know more about bitarray.