ASCII字符的矩阵框

时间:2016-01-15 02:20:57

标签: python encoding

这是我发布堆栈溢出的第一篇文章。取笑我自己的乐趣。

这是我输入的用于编码“真正的人类和真正的英雄”的文字。

我输入的用于解码的文本是编码消息的结果:“W6){w#6~,$ w%6x {%} B6w%z6w6){w#6~ {)& D”

我真的只是想知道这些盒子在最后一个关键值的末尾是什么(96 - 100。)

似乎它们是引用字符的序数值的矩阵,但它们不显示实际字符。

这是python代码:

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

1 个答案:

答案 0 :(得分:0)

这些通常是“不可打印”的角色。其中一些具有特殊含义

例如,chr(30)表示CAN(取消)

在我的终端上,它打印成一个带有001E的小盒子的盒子,里面是十六进制(30)