基礎拉丁字母
| 字元 | 代碼 | 字元 | 代碼 | 字元 | 代碼 | 字元 | 代碼 | 字元 | 代碼 | 字元 | 代碼 | 字元 | 代碼 | 
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| A | ·- | B | -··· | C | -·-· | D | -·· | E | · | F | ··-· | G | --· | 
| H | ···· | I | ·· | J | ·--- | K | -·- | L | ·-·· | M | -- | N | -· | 
| O | --- | P | ·--· | Q | --·- | R | ·-· | S | ··· | T | - | U | ··- | 
| V | ···- | W | ·-- | X | -··- | Y | -·-- | Z | --·· | 
數字
摩斯電碼的數字有長碼和短碼,通常用長碼,於中文電碼中組成漢字則使用短碼。
| 字元 | 長碼 | 短碼 | 字元 | 長碼 | 短碼 | 字元 | 長碼 | 短碼 | 字元 | 長碼 | 短碼 | 字元 | 長碼 | 短碼 | 
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | ·---- | ·- | 2 | ··--- | ··- | 3 | ···-- | ···- | 4 | ····- | ····- | 5 | ····· | ····· | 
| 6 | -···· | -···· | 7 | --··· | -··· | 8 | ---·· | -·· | 9 | ----· | -· | 0 | ----- | - | 
標點符號
| 字元 | 代碼 | 字元 | 代碼 | 字元 | 代碼 | 字元 | 代碼 | 字元 | 代碼 | 字元 | 代碼 | 
|---|---|---|---|---|---|---|---|---|---|---|---|
| . | ·-·-·- | : | ---··· | , | --··-- | ; | -·-·-· | ? | ··--·· | = | -···- | 
| ‘ | ·----· | / | -··-· | ! | -·-·-- | – | -····- | _ | ··--·- | “ | ·-··-· | 
| ( | -·--· | ) | -·--·- | $ | ···-··- | & | ·-··· | @ | ·--·-· | + | ·-·-· | 
代碼如下
codelm={}
codelm['A']='01'
codelm['B']='1000'
codelm['C']='1010'
codelm['D']='100'
codelm['E']='0'
codelm['F']='0010'
codelm['G']='110'
codelm['H']='0000'
codelm['I']='00'
codelm['J']='0111'
codelm['K']='101'
codelm['L']='0100'
codelm['M']='11'
codelm['N']='10'
codelm['O']='111'
codelm['P']='0110'
codelm['Q']='1101'
codelm['R']='010'
codelm['S']='000'
codelm['T']='1'
codelm['U']='001'
codelm['V']='0001'
codelm['W']='011'
codelm['X']='1001'
codelm['Y']='1011'
codelm['Z']='1100'
codelm['0']='11111'
codelm['1']='01111'
codelm['2']='00111'
codelm['3']='00011'
codelm['4']='00001'
codelm['5']='00000'
codelm['6']='10000'
codelm['7']='11000'
codelm['8']='11100'
codelm['9']='11110'
codelm[',']='110011'
codelm[';']='101010'
codelm['?']='001100'
codelm['=']='10001'
codelm["'"]='011110'
codelm['/']='10010'
codelm['!']='101011'
codelm['-']='100001'
codelm['_']='001101'
codelm['"']='010010'
codelm['(']='10110'
codelm[')']='101101'
codelm['$']='0001001'
codelm['&']='01000'
codelm['@']='011010'
codelm['+']='01010'
codeml={}
codeml['01']='A'
codeml['1000']='B'
codeml['1010']='C'
codeml['100']='D'
codeml['0']='E'
codeml['0010']='F'
codeml['110']='G'
codeml['0000']='H'
codeml['00']='I'
codeml['0111']='J'
codeml['101']='K'
codeml['0100']='L'
codeml['11']='M'
codeml['10']='N'
codeml['111']='O'
codeml['0110']='P'
codeml['1101']='Q'
codeml['010']='R'
codeml['000']='S'
codeml['1']='T'
codeml['001']='U'
codeml['0001']='V'
codeml['011']='W'
codeml['1001']='X'
codeml['1011']='Y'
codeml['1100']='Z'
codeml['11111']='0'
codeml['01111']='1'
codeml['00111']='2'
codeml['00011']='3'
codeml['00001']='4'
codeml['00000']='5'
codeml['10000']='6'
codeml['11000']='7'
codeml['11100']='8'
codeml['11110']='9'
codeml['010101']='.'
codeml['111000']=':'
codeml['110011']=','
codeml['101010']=';'
codeml['001100']='?'
codeml['10001']='='
codeml['011110']="'"
codeml['10010']='/'
codeml['101011']='!'
codeml['100001']='-'
codeml['001101']='_'
codeml['010010']='"'
codeml['10110']='('
codeml['101101']=')'
codeml['0001001']='$'
codeml['01000']='&'
codeml['011010']='@'
codeml['01010']='+'
def l2m(str):
    mos=''
    for l in str.upper():
        if l==' ':mos+='    '
        else:
            if l in codelm:
                mos=mos+codelm[l]+'   '
    return mos[:-3].replace('1','-').replace('0','.')
def m2l(mos):
    str=''
    mos=mos.replace('.','0').replace('-','1')
    for word in mos.split('       '):
        for l in word.split('   '):
            if l in codeml:
                str+=codeml[l]
        str+=' '
    return str
str=input("請輸入要傳送的句子 : ")
mos=l2m(str)
print(mos)
str=m2l(mos)
print(str)
結果
請輸入要傳送的句子 : Hello, I am Thomas .... . .-.. .-.. --- --..-- .. .- -- - .... --- -- .- ... HELLO, I AM THOMAS
