I'm trying to get a string, jJ
, as input and then converting each character to it's 6 bit binary form using the mapping given in a and concatenating them and returning it in mapFirst(string)
. That is, jJ
becomes 100011001001
.
In binaryToLetter(string)
I'm taking the returned value and separating it into parts of 8 bits and converting it back to it's character form and concatenated. 100011001001
becomes 00001000
and 11001001
, which are then converted and joined to give (backspace)É
.
In my code, I'm getting the error :
Exception has occurred: ValueError
invalid literal for int() with base 2: ''
File "C:\Users\Sembian\Desktop\exc files new\Ex_Files_Learning_Python\Exercise Files\task_cs\task1.py", line 11, in <genexpr>
return ''.join( str( int( (binNew[newLen:newLen-i]),2 ) ).replace('0b','').zfill(8) for i in range(0, newLen, n) )
The code I used is :
from textwrap import wrap
a = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/']
def mapFirst(string):
return ''.join(str(bin(ord(chr(a.index(c))))).replace('0b','').zfill(6) for c in string)
def binaryToLetter(binNew):
newLen = len(str(binNew))
n=8
return ''.join( str( int( (binNew[newLen:newLen-i]),2 ) ).replace('0b','').zfill(8) for i in range(0, newLen, n) )
def main():
k = 'jJ'
print("the first binary value is: ",mapFirst(k))
print("the final decoded value is: ", binaryToLetter(mapFirst(k)))
if __name__ == "__main__":
main()