Python ASCII字符中的矩阵框
这是我第一篇关于堆栈溢出的文章。取笑我是为了你自己的乐趣 这是我输入的用于编码“一个真正的人,一个真正的英雄”的文本 我输入的用于解码的文本是经过编码的消息的结果:“W6{w#6~,$w%6x{%}B6w%z6w6{w#6~{)&D” 我真的很想知道最后一个键值(96-100)末尾的那些框是什么 似乎它们是引用字符序数值的矩阵,但却不显示实际字符 以下是python代码:Python ASCII字符中的矩阵框,python,encoding,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 enc
'''
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
“”
此程序接受用户输入以创建和显示编码消息,然后再次接受用户输入以创建和显示多个解码消息。
'''
#-----功能--------
#这是对消息进行编码的函数
def编码(TEXTFORENCODE,键):
#变量等于空字符串,因此可以向其中添加字符
encodedMessage=“”
#这个循环遍历文本中的每个字符并创建一个编码消息
对于textForEncoding中的字符:
#如果字符加键的序数值大于126,则将运行此操作
如果ord(字符)+int(键)>126:
#移动原始字符的序数值并将其设置为变量
newCharacter=ord(字符)+int(键)-127+32
#将新字符的序数值转换为实际字符,并将其设置为变量
encodedCharacter=chr(newCharacter)
#编码文本现在等于其自身加上编码字符
encodedMessage+=encodedCharacter
#如果字符加键的序数值小于或等于126,则将运行此操作
其他:
#移动原始字符的序数值并将其设置为变量
newCharacter=ord(字符)+int(键)
#将新字符的序数值转换为实际字符,并将其设置为变量
encodedCharacter=chr(newCharacter)
#编码文本现在等于其自身加上编码字符
encodedMessage+=encodedCharacter
#返回encodedMessage变量
返回编码消息
#这是解码消息的功能
def解码(文本编码,按键):
#变量等于空字符串,因此可以向其中添加字符
decodedMessage=“”
#这个循环遍历文本中的每个字符并创建一条解码消息
对于textForDecoding中的字符:
#如果字符减去键的序数值小于32,则将运行此操作
如果ord(字符)-int(键)<32:
#移动原始字符的序数值并将其设置为变量
newCharacter=ord(字符)-int(键)+127-32
#将新字符的序数值转换为实际字符,并将其设置为变量
decodedCharacter=chr(newCharacter)
#解码文本现在等于自身加上解码字符
decodedMessage+=decodedCharacter
#如果字符减去键的值大于或等于32,将运行此操作
其他:
#移动字符的序数值并将其设置为变量
newCharacter=ord(字符)-int(键)
#将新字符的序数值转换为实际字符,并将其设置为变量
decodedCharacter=chr(newCharacter)
#解码文本现在等于自身加上解码字符
decodedMessage+=decodedCharacter
#返回decodedMessage变量
返回解码消息
#------初步执行--------
#获取要编码的消息的用户输入
textforencode=input(“\n请输入要编码的文本:”)
#当键的值不是1到100之间的数字时,这将循环
尽管如此:
#用户设置键的值
key=input(“\n请输入一个介于1和100之间的键值:”)
#如果密钥是介于1和100之间的数字。。。
如果key.isdigit()和(int(key)>=1和int(key)这些字符通常是“不可打印”字符。其中一些字符有特殊含义
例如,chr(30)表示可以(取消)
在我的终端上,它打印成一个盒子,里面有一个小盒子,里面有001E,这是十六进制(30)a谢谢!