首页 > 解决方案 > 字符串 -> ASCII+1 -> 位串

问题描述

项目:将每个字符的数字 ASCII 值加 1。将其转换为位字符串。将该字符串的位向左移动一位。加密字符串中的单个空格字符分隔生成的位字符串。

程序输入输出示例如下所示:

输入消息:世界你好!

0010011 1001101 1011011 1011011 1100001 000011 1110001 1100001 1100111 1011011 1001011 000101

# Put your code here
string = input("Enter message: ")

#Convert string from ASCII to Decimal
A_string = [ord(c) for c in string]
print(A_string)

# add 1 to ASCII value 
B_string = A_string
for i in range(len(B_string)):
    B_string[i] = B_string[i] + 1 
print(B_string)


#Decimal to Binary
decimal = B_string
remainder = decimal
Binary_string = decimal

for i in range(len(decimal)):
    remainder[i] = int(decimal[i])
    remainder[i] %= 2
    decimal[i] = decimal[i] // 2
    Binary_string[i] = str(remainder[i] + Binary_string[i])
print(Binary_string)


#Shift Left
length = len(Binary_string)-1
start = 1
shiftLeft = ''
while length !=0:
    shiftLeft = shiftLeft + Binary_string[start] + " "
    length -= 1
    start += 1
shiftLeft += Binary_string[0]
print(shiftLeft)


我的结果:输入消息:Hello world!

[72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33, 92]
[73, 102, 109, 109, 112, 33, 120, 112, 115, 109, 101, 34, 93]
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
0 0 0 0 0 0 0 0 0 0 0 0 0

****最大的目标是做这个老派,即不使用内置函数,而是使用循环

标签: pythonstringtype-conversionbit-manipulationascii

解决方案


在下面的代码示例中,输入字符串被转换为 ASCII 值列表,这些值通过加一来移位。然后将该值转换回字符串并放入名为 的列表中shiftedChars。最后,这些字符串被转换成二进制。

inputString = input("Enter the input string: ")
asciiValues = []
shiftedChars = []
results = []

# loop through each character in the input string
for character in inputString:
    value = ord(character) + 1                           #convert to ascii and shift by 1
    asciiValues.append(value)                            #add to list of ascii values

# loop through each value in the list
for asciiValue in asciiValues:
    shiftedChars.append(chr(asciiValue))                 #convert back to string and store in another list

# loop through each character in the shiftedChar list
for shiftedChar in shiftedChars:
    res = "".join(f"{ord(shiftedChar):08b}")             #convert to binary
    results.append(res)                                  #store result

#print results
for result in results:
    print(result, end=" ")  

运行:

Enter the input string: Hello world

输出:

01001001 01100110 01101101 01101101 01110000 00100001 01111000 01110000 01110011 01101101 01100101

哪些是Ifmmp!xpsme二进制序列中的移位字符。

编辑: - -

从字符串转换为二进制的一些额外解释。尽管它可能看起来非常混乱的语法,但希望这将有助于理清思路。

String -> Ascii    - use ord()
Ascii - > Char     - use chr()
Char -> Binary     - convert char using ord() to ascii then format into binary

因此,对于将 char 转换为二进制的最后一种方法,我们需要首先获得 ascii 数值,然后我们将.format用于转换。

现在看看res = "".join(f"{ord(shiftedChar):08b}")我们可能能够将它分解成更小的部分,并发现一些代码可能不是必需的。

让我们看一个较小的示例:

print(ord("a"))                         #ord("a")= 97 | using for the shiftedChar
print(format(97, '08b'))                #01100001
emptyString = ""
print(emptyString.join(f"{97:08b}"))    #01100001

如您所见,该format方法接受除了 ascii 值之外的第二个参数。这用于告诉如何格式化最终结果。有不同形式的二进制,例如不带前导 0 或以 . 开头的序列0b。在空字符串上使用.join似乎主要是为了将其保留为字符串。希望这能解释发生了什么。


推荐阅读