1
$\begingroup$

I am trying to get a binary string that has been converted from text of a text file, I am able to get that but the problem is, I need each character to be represented by same number of bits, but that is not what I get (please see the below python code and corresponding output). For example, character i is represented by 1101001, which is 7 bits long, but character ! is represented by 100001, which is 6 bits long.

Is there any encoding/decoding system where each character takes same amount of bits?

content = open('a.txt', 'r').read()
test_str = content
# using join() + ord() + format()  ... Converting String to binary 

Binary = ' '.join(format(ord(i), 'b') for i in test_str)

#Decimal=int(Binary, 2)



# printing original string  
print("The original string is : " + str(test_str)) 
# printing result  
print("The string after Binary conversion : \n" + str(Binary))

Output:

The original string is : Hi! Is there a solution?
The string after Binary conversion : 
1001000 1101001 100001 100000 1001001 1110011 100000 1110100 1101000 1100101 1110010 1100101 100000 1100001 100000 1110011 1101111 1101100 1110101 1110100 1101001 1101111 1101110 111111
$\endgroup$
1
  • 1
    $\begingroup$ This is a python-specific programming question. The C counterpart supports obligatory leading zeroes (e.g. %07b in systems that support %b), and the python format likely also has such a mechanism. If not, it is easy to implement it yourself. You're a programmer! Be creative. $\endgroup$ – Yuval Filmus Mar 15 '20 at 22:30
0
$\begingroup$

The usual way to solve this problem is by adding leading zeroes. So i would still be represented by 1101001, while ! will be represented by 0100001.

This is similar to how you digital clock might use 06:40 for 6:40, or 12:05 for 12:5.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.