Carlos Mediavilla
Carlos Mediavilla

Reputation: 302

Make an octal to UTF-8 converter (english alphabet only). What are octal scaped characters

I'm doing a discord bot in python (using discord.py API) and have been make it work with hex, morse and base64/32 strings, I'm now trying to make it decode octal strings.

I've been doing some research and found this post Python - Convert Octal to non-English Text from file and other similar posts that try to convert non-english characters with something called 'octal escape characters'. I understand what an escaped character is, I know what an octal character is, but what I don't know is what those escaped characters do.

Anyway, I just need it to read a plain octal string and decode it to words. There are no foreign characters involved, so I really hope there is an easier way to do it

110 145 154 154 157 40 151 156 164 145 162 156 145 164 for example should be translated to 'hello internet'

I found other posts with english charaters only but they are all for java. This is what i've got so far:

    #Bot is listening for message. If it finds one message which starts with !decodeoctal It'll execute the inside of the conditional (the decoder itself)
    if message.content.startswith('!decodeoctal'):

        #The content of the message is now a list inside a variable called msglist               
        msglist = message.content.split()

        #The first item of the list (the one that contains the !decodeoctal string) is discarded leaving the rest of list, that contains the octal string intact
        msglist = msglist[1:]                       
            try:    
                #Code dor decoding the string goes here                                                 
            except:                         
                msg = '''Decoding error.'''
                await client.send_message(message.channel, msg)

The code is one level indented because it goes inside another if statement. Any ideas?

Upvotes: 0

Views: 658

Answers (1)

Matthias
Matthias

Reputation: 13232

You need chr to convert numbers to characters.

msglist is a list of strings with the string values '110', '145', '154' and so on. If you want feed them to chr you have to create numbers from those values. That's where int comes in. int has a parameter for the base which has to be 8 because you have octal values.

values = []
for octal_string in msglist:
    number = int(octal_string, base=8)
    values.append(number)
print(''.join(chr(value) for value in values))

Short version:

print(''.join(chr(int(octal_string, base=8)) for octal_string in msglist))

As requested in the comment here is some more information about int. int converts a string to a number. '100' and 100 are two different things. Just try print(99 < 100) and print('99' < '100'). Using int is simple: int('100') will give us 100.

But what if '100' wasn't meant to be a decimal number but binary? int still can do the conversion but we will have to specify the base (which is 10 by default). So now we use int('100', base=2) and get 4.

In an octal system '100' would lead to the decimal value 64 (int('100', base=8)) and 100 in hexadecimal would be the decimal value 256 (int('100', base=16)).

Upvotes: 1

Related Questions