Jun-17-2022, 09:44 PM
Hi,
This simple encoding code converts text to ASCII and adds a one digit number to it. It seems to work ok, but while testing it, I noticed that if a 'v' is used, it's converted to ' ' when a 9 is added to it. (probably bc it's hitting ascii 127 - delete). Is there a way to fix it?
TIA
This simple encoding code converts text to ASCII and adds a one digit number to it. It seems to work ok, but while testing it, I noticed that if a 'v' is used, it's converted to ' ' when a 9 is added to it. (probably bc it's hitting ascii 127 - delete). Is there a way to fix it?
TIA
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
encoded_txt = '' key = 0 def encode_text(): global encoded_txt, key arry = [] txt = input ( "Enter an alphanumeric text: " ) while True : try : key = int ( input ( 'Enter a single digit number: ' )) if key > 9 : n = 1 / 0 # force to fail break except : print ( "That's not a valid option!" ) # fill array with entered text for _ in range ( len (txt)): arry.append(txt[_]) # add pass to characters for char in range ( len (arry)): x = ord (arry[char]) + key # convert ascii to char encoded_txt + = chr (x) print ( "Encoded text:" , encoded_txt) encode_text() |
Output:Enter an alphanumeric text: vince
Enter a single digit number: 9
New text: rwln
Decoded text: vince