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
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
