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
%07b
in systems that support%b
), and the pythonformat
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