简体   繁体   中英

Matrix boxes in ASCII characters

This is my first post to stack overflow. Make fun of me for your own fun.

This is the text I enter for encoding "A real human being, and a real hero."

The text I entered for decoding is the result of the encoded message: "W6){w#6~,$w%6x{ %}B6w%z6w6){w#6~{)&D"

I'm really just wondering what those boxes are at the end of the last key values (96 - 100.)

It seems as if they are matrices that reference the ordinal value of the character, yet they don't display the actual character.

Here is the python code:

'''
This program takes user input to create and display an encoded message, and then takes user input again to create and display multiple decoded messages. 
'''

# -------- Functions --------

# This is the function for encoding a message
def encode (textForEncoding, key):

    # The variable equals a null string so characters can be added to it
    encodedMessage = ""

    # This loop goes though each character in the text and creates an encoded message
    for character in textForEncoding:

        # This will run if the ordinal value of the character plus the key is greater than 126
        if ord(character) + int(key) > 126:

            # Shifts the ordinal value of the original character and sets it equal to a variable
            newCharacter = ord(character) + int(key) - 127 + 32

            # Converts the ordinal value of the new character into an actual character and sets it equal to a variable 
            encodedCharacter = chr(newCharacter)

            # The encoded text is now equal to itself plus the encoded character 
            encodedMessage += encodedCharacter

        # This will run if the ordinal value of the character plus the key is less than or equal to 126
        else:

            # Shifts the ordinal value of the original character and sets it equal to a variable
            newCharacter = ord(character) + int(key)

            # Converts the ordinal value of the new character into an actual character and sets it equal to a variable 
            encodedCharacter = chr(newCharacter)

            # The encoded text is now equal to itself plus the encoded character 
            encodedMessage += encodedCharacter

    # Returns the encodedMessage variable
    return encodedMessage

# This is the function for decoding a message
def decode (textForDecoding, key):

    # The variable equals a null string so characters can be added to it
    decodedMessage = ""

    # This loop goes though each character in the text and creates a decoded message
    for character in textForDecoding:

        # This will run if the ordinal value of the character minus the key is less than 32
        if ord(character) - int(key) < 32:

            # Shifts the ordinal value of the original character and sets it equal to a variable
            newCharacter = ord(character) - int(key) + 127 - 32

            # Converts the ordinal value of the new character into an actual character and sets it equal to a variable 
            decodedCharacter = chr(newCharacter)

            # The decoded text is now equal to itself plus the decoded character 
            decodedMessage += decodedCharacter

        # This will run if the value of the character minus the key is greater than or equal to 32
        else:

            # Shifts the ordinal value of the character and sets it equal to a variable
            newCharacter = ord(character) - int(key)

            # Converts the ordinal value of the new character into an actual character and sets it equal to a variable 
            decodedCharacter = chr(newCharacter)

            # The decoded text is now equal to itself plus the decoded character 
            decodedMessage += decodedCharacter

    # Returns the decodedMessage variable
    return decodedMessage

# -------- Initial Execution --------

# Gets user input for the message to encode
textForEncoding = input("\n Please enter text to encode: ")

# This will loop while the value for key is not a digit between 1 and 100
while True:

    # User sets the value for key
    key = input("\n Please enter a key value between 1 and 100: ")

    # If the key is a digit between 1 and 100...
    if key.isdigit() and (int(key) >= 1 and int(key) <= 100):

        # ... The loop will break
        break

    # If the key is not not a digit between 1 and 100...
    else:

        # ... An error will print, and the loop will continue
        print("\nError: Please enter a positive number between 1 and 100.")

# Sets the encoded text equal to the result of the encode function
encodedText = encode(textForEncoding, key)

# Prints the encoded message 
print("\n The encoded message is %s " %encodedText)

# Gets user input for the message to decode
textForDecoding = input("\n Enter an encoded message to decode: ")

# Prints a message before decoded messages
print("\n The following are decoded messages for key values 1 to 100:")

# This loop runs through key values from 1 to 100, and displays the decoded messages
for key in range (1, 101):

    # Sets the decoded text equal to the result of the decode function
    decodedText = decode(textForDecoding, key)

    # Prints the key value and decoded message
    print("\n  Key: %i - decoded message: %s \n" %(key, decodedText))

# End

Those are normally "unprintable" characters. Some of them have special meanings

For example chr(30) means CAN (cancel)

On my terminal it prints as a box with a tiny box with 001E in it, which is hex(30)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM