如何使用列表和字典将字符串转换为二进制代码

Posted

技术标签:

【中文标题】如何使用列表和字典将字符串转换为二进制代码【英文标题】:How to convert a string to binary code using list and dictionary 【发布时间】:2019-08-13 23:22:28 【问题描述】:

我正在尝试将字符串转换为 char 数组,然后循环遍历数组中的每个 char 并与字典键进行比较,然后如果它们匹配,则打印键值并移动到数组中的下一个字符

我收集了一些例子并试图在脑海中解决它。我是 python 新手,经过很长时间尝试教我 10 岁的儿子后,我又回到了编程作为一种爱好。他在谈论二进制,我说我们可以用 python 编写一个程序,例如可以将他的名字作为输入并以二进制代码打印他的名字。

 #binary table is formatted such i have shortened it for simplicity

    binaryTable =   
       "a" : "01100001",
       "b" : "01100010",
       "c" : "01100011",
       "d" : "01100100"
    

    word = input('please input a value to see its representation in    Binary Code: ')

    def split(letters): 
        return [char for char in letters]

    def members(dictArg, keysListArg):
        count = 0
        for x in newArray:
            if newArray[x] == binaryTable.keys():
                value = binaryTable.keys()
                print(newArray[x])
                print(' : ')
                print(value)
                count += 1
        return count

   def printBinary(dictArg, keysListArg): 
      count = 0 
      for list_item in keysListArg: 
        if list_item in dictArg:
            count+= 1
            print(count)
            print(list_item)
            print(' : ')
            #print(keysListArg)
            #print(dictArg) #print dictArg.Key() How to do this
            print('\n')
      return count


    print('testing printBinary function\n')

    newArray = split(word)
    #members(newArray, binaryTable)
    printBinary(newArray. binaryTable)

如果我说 abcd 我希望输出是 一个新的字典,其中的字符在 word 中带有它们的二进制键代表,或者只是在 for 循环迭代中打印字母键 #next 到代表性的二进制值,例如

 j : 0110101
 a : 0101010
 m : 0101010
 e : 0101010
 s : 0101010

output:


1
a
 : 


2
e
 : 


3
j
 : 


4
m
 : 


5
s
 : 


['j', 'a', 'm', 'e', 's']
>>> 

*为什么键出来的顺序不一样?

【问题讨论】:

什么版本的python? dict 用于在迭代时不保证键的顺序。不确定现在的行为。 ***.com/questions/5629023/… 3.7 我这样编辑了函数,但它仍然以错误的顺序打印: def printBinary(dictArg, keysListArg): count = 0 for list_item in keysListArg: if list_item in dictArg: count+= 1 print(count) print(list_item) print(' : ') x = keysListArg. get(list_item) print(x) 返回计数 我很难理解你在追求什么......但你的意思是把函数 printBinary 称为 printBinary(binaryTable, newArray) 而不是 printBinary(newArray, binaryTable) 吗? 我已经解决了: 【参考方案1】:
#program to take a string and show the binary representation of all the letters
binaryTable =   
   "a" : "01100001",
   "b" : "01100010",
   "c" : "01100011",
   "d" : "01100100",
   "e" : "01100101",
   "f" : "01100110",
   "g" : "01100111",
   "h" : "01101000",
   "i" : "01101001",
   "j" : "01101010",
   "k" : "01101011",
   "l" : "01101100",
   "m" : "01101101",
   "n" : "01101110",
   "o" : "01101111",
   "p" : "01110000",
   "q" : "01110001",
   "r" : "01110010",
   "s" : "01110011",
   "t" : "01110100",
   "u" : "01110101",
   "v" : "01110110",
   "w" : "01110111",
   "x" : "01111000",
   "y" : "01111001",
   "z" : "01111010",
   "A" : "01000001",
   "B" : "01000010",
   "C" : "01000011",
   "D" : "01000100",
   "E" : "01000101",
   "F" : "01000110",
   "G" : "01000111",
   "H" : "01001000",
   "I" : "01001001",
   "J" : "01001010",
   "K" : "01001011",
   "L" : "01001100",
   "M" : "01001101",
   "N" : "01001110",
   "O" : "01001111",
   "P" : "01010000",
   "Q" : "01001111",
   "R" : "01010010",
   "S" : "01010011",
   "T" : "01010100",
   "U" : "01010101",
   "V" : "01010110",
   "W" : "01010111",
   "X" : "01011000",
   "Y" : "01011001",
   "Z" : "01011010"


print('Hello there! I am a binary translator. Add some text to see it in binary!')
print('Below is a binary table for all characters of the Alphabet. Remember, upper and lower case characters have different binary values!')
print(
'''
   Letter    Binary      Letter  Binary
     a          01100001    A   01000001
     b      01100010    B   01000010
     c      01100011    C   01000011
     d      01100100    D   01000100
     e      01100101    E   01000101
     f      01100110    F   01000110
     g      01100111    G   01000111
     h      01101000    H   01001000
     i      01101001    I   01001001
     j      01101010    J   01001010
     k      01101011    K   01001011
     l      01101100    L   01001100
     m      01101101    M   01001101
     n      01101110    N   01001110
     o      01101111    O   01001111
     p      01110000    P   01010000
     q      01110001    Q   01010001
     r      01110010    R   01010010
     s      01110011    S   01010011
     t      01110100    T   01010100
     u      01110101    U   01010101
     v      01110110    V   01010110
     w      01110111    W   01010111
     x      01111000    X   01011000
     y      01111001    Y   01011001
     z      01111010    Z   01011010''')


word = input('please input a value to see its representation in Binary Code: ')

#splits the word into single letters and stores in a list aka newArray
def split(letters): 
    return [char for char in letters]

#takes the parameters of newArray and binaryTable.
#new array is the input word split into letters
def printBinary(dictArg, keysListArg): 
  for list_item in dictArg: 
    if list_item in keysListArg:
        #grabs the binary value linked to its key and stores in x
        x = keysListArg.get(list_item)
        #prints list item along side its binary key
        print(list_item + ' : ' + x)

#splits the word
newArray = split(word)
#prints each letter next to its binary code representation
printBinary(newArray, binaryTable)

样本输出:

please input a value to see its representation in Binary Code: louis
l : 01101100
o : 01101111
u : 01110101
i : 01101001
s : 01110011
>>> 

【讨论】:

以上是关于如何使用列表和字典将字符串转换为二进制代码的主要内容,如果未能解决你的问题,请参考以下文章

Python学习之旅---数据类型(数字字符窜列表元组字典布尔值)

Python:将字符串转换为十进制数

将CSV文件转换为Python字典,数组和二进制文件

我正在使用二进制阅读器将字符串转换为字节,如何将字节反转回相同的字符串 [重复]

Python 数字字符串列表元祖字典集合

如何在Python中将十六进制字符串转换为ObjectId [重复]