0

I'm currently working on this code now..

message = input("Enter ASCII codes: ")

decodedMessage = ""

for item in message.split():
   decodedMessage += chr(int(item))   

print ("Decoded message:", decodedMessage)

#1st Run:
Enter ASCII codes: 97
Decoded message: a
#2nd Run:
Enter ASCII codes: 9797
Decoded message: ♅

The result I want is: aa which is from 97 and another 97 of ascii codes. How can this be done?

I want it like this.

ord_username = input("Enter Username:")
letters = str(ord_username)
ordlist=[]
z=""

for letter in letters:
    number = ord(letter)
    ordlist.append(number)


for i in ordlist:
    z += str(i) + ""
print (z)

#1st Run:
Enter Username:a
97
#2nd Run:
Enter Username:ab
9798
Franz
  • 3
  • 3
  • 3
    So how do you split `1111`, as `ascii(11)ascii(11)` or `ascii(1)ascii(111)` or `ascii(111)ascii(1)`? Isn't it ambiguous? – Bhargav Rao Mar 04 '15 at 21:10
  • 1
    Put a space between your numbers, for goodness sake. – Fred Larson Mar 04 '15 at 21:12
  • Worth adding that this is Python3-ish. On Python 2 passing anything outside the ASCII range to `chr()` gives a `TypeError`. – Iguananaut Mar 04 '15 at 21:15
  • Actually, my teacher does not allow me that @FredLarson – Franz Mar 04 '15 at 21:19
  • How is it supposed to know where to split, then? Are you supposed to slice instead of split perhaps? – Fred Larson Mar 04 '15 at 21:20
  • Split is not your answer. – Katpoes Mar 04 '15 at 21:23
  • I was wondering if you were looking for something like this: http://stackoverflow.com/q/9475241/10077. But your edit has me confused. – Fred Larson Mar 04 '15 at 21:28
  • Im thinking of a way to refer to ascii codes 65-90 which is A-Z and 97-122 which is a-z.because we are told to input a number and convert it to alphabet using chr().So sorry i cant explain it well. – Franz Mar 04 '15 at 21:41
  • It's like this way (http://stackoverflow.com/questions/22227631/how-do-i-convert-a-list-of-numbers-into-their-corresponding-chr?rq=1). – Franz Mar 04 '15 at 21:45

1 Answers1

1

You want to interpret your string as being composed of 3-digit strings beginning with a '1' or 2-digit strings that cannot begin with a 1. Because of that, you can move through the string from start to finish, plucking out three characters if the first is a 1 or two characters if it isn't. This just needs a simple method:

def stringsplit(message):
    message_split = []
    while len(message) > 0:
        if message[0] == '1':
                message_split.append(message[:3])
                message = message[3:]
        else:
                message_split.append(message[:2])
                message = message[2:]
    return message_split

Then, replace for item in message.split(): with for item in stringsplit(message):

Caveat: Python 2.7 implementation; your mileage may vary, but it shouldn't.

Tom
  • 570
  • 5
  • 19