python实现的摩斯电码解码\编码器
详细说明:
现在这年头谍战片、警匪片动不动就用摩斯密码来传递信息,一方面可以用来耍帅,另外一方面好像不插入这样子一个情节就
显得不够专业了;那么摩斯密码(实际上应该是摩斯电码,但是不少人都喜欢把它叫做摩斯密码,这样比较有神秘感,显得高大上。)究竟是什么呢?一个不专业但是很直观的解释就是:摩斯电码是用点"."和横"-"的不同组合来表示数字和字母。
具体一点就是下图那样:


看,就是这么简单!
什么?记不住?没关系,有人总结出了一些规律来帮助记忆,
如下图:

如果你还是记不住,但是又想用拿它来发一下信息、玩一下,那你可以
自己写一个摩斯电码的解码、编码器呀。
本项目只需用到一个用于写GUI界面的第三方库wx,除去空行和注释,一共不到200行代码。
(代码在python2.7或python3.6下均能正常运行,已在以下环境中进行过测试:
python2.7 +wx2.8; python3.6 + wx4.0)
这个简易的摩斯电码编码/解码器如下:

项目结构图:
整体的项目结构十分简单,只有一个脚本文件,另外一个是根据脚本进行编译后的windows系统下的可执行程序,用户的机器甚至无需python环境便可使用,即装即用。
如下:

准备工作:
安装必要的第三方库:
pip install wxPython
实现过程的部分代码展示
- 摩斯电码表实际上就是一本字典,字符\数字和电码(点划)有着一一对应的关系,
在python中用dict来构建十分方便。
比如,你可以用以下的方式规规矩矩地构建一个摩斯电码表.
CODE = {'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': '--..',
'0': '-----', '1': '.----', '2': '..---',
'3': '...--', '4': '....-', '5': '.....',
'6': '-....', '7': '--...', '8': '---..',
'9': '----.'
}
你也可以用下面的方式来构建字典,
Keys = 'abcdefghijklmnopqrstuvwxyz0123456789'
Values = ['.-','-...','-.-.','-..','.','..-.','--.','....',
'..','.---','-.-','.-..','--','-.','---','.--.',
'--.-','.-.','...','-', '..-','...-','.--','-..-',
'-.--','--..','-----','.----','..---','...--',
'....-','.....','-....','--...','---..','----.']
CODE = dict(zip(Keys.upper(), Values))
然后是导入相关的库:
import wx
import os
from wx.lib.wordwrap import wordwrap
import wx.lib.dialogs
只用到一个第三方库:wxPython,该库用于编写程序的GUI.
2.编写界面:
class MainWindow(wx.Frame):
def __init__(self,parent,title):
wx.Frame.__init__(self,parent,title=title,size=(800,400))
font = wx.Font(15, wx.SWISS, wx.NORMAL, wx.NORMAL)
self.contents = wx.TextCtrl (self,style=wx.TE_MULTILINE | wx.HSCROLL )
self.contents.SetBackgroundColour((80,180,30))
self.coder = wx.TextCtrl (self,style=wx.TE_MULTILINE | wx.HSCROLL )
self.coder.SetBackgroundColour((20,200,100))
self.msgFont = self.contents.GetFont()
self.msgColour = wx.BLACK
self.contents.SetFont(font)
self.coderFont = self.coder.GetFont()
self.coderColour = wx.BLACK
self.coder.SetFont(font)
self.findData = wx.FindReplaceData()
"""创建状态栏"""
self.CreateStatusBar()
"""file菜单布局"""
filemenu = wx.Menu()
menuNew = filemenu.Append(wx.ID_NEW ,"&New\tCtrl+N","New a file 新建")
menuOpen = filemenu.Append(wx.ID_OPEN ,"&Open\tCtrl+O","Open a file 打开")
menuSave = filemenu.Append(wx.ID_SAVE ,"&Save\tCtrl+S","Save the file 保存")
"""菜单分隔线"""
filemenu.AppendSeparator()
menuExit = filemenu.Append(wx.ID_EXIT ,"E&xit\tCtrl+Q","Tenminate the program 退出")
"""格式菜单布局"""
formatmenu = wx.Menu ()
menuMsgFont = formatmenu.Append(wx.ID_ANY ,"&msg Font","Set the message font 设置输入字体")
menuCoderFont = formatmenu.Append(wx.ID_ANY ,"&coder Font","Set the coder font 设置输出字体")
"""帮助菜单布局"""
helpmenu = wx.Menu ()
menuhelpdoc = helpmenu.Append(wx.ID_ANY ,"usage\tF1","usage 使用说明")
"""菜单栏布局"""
menuBar = wx.MenuBar ()
menuBar.Append(filemenu,"&File")
menuBar.Append(formatmenu,"&Setup")
menuBar.Append(helpmenu,"&Help")
self.SetMenuBar(menuBar)
"""创建按钮"""
encoderButton = wx.Button (self,label = 'Encode')
decoderButton = wx.Button (self,label = 'Decode')
"""函数绑定"""
self.Bind(wx.EVT_MENU,self.OnExit,menuExit)
self.Bind(wx.EVT_MENU,self.OnOpen,menuOpen)
self.Bind(wx.EVT_MENU,self.OnSave,menuSave)
self.Bind(wx.EVT_MENU,self.OnNew,menuNew)
self.Bind(wx.EVT_MENU,self.OnSelectFont,menuMsgFont)
self.Bind(wx.EVT_MENU,self.OnSelectCoderFont,menuCoderFont)
self.Bind(wx.EVT_MENU,self.Onhelpdoc,menuhelpdoc)
self.Bind(wx.EVT_BUTTON,self.Encode,encoderButton)
self.Bind(wx.EVT_BUTTON,self.Decode,decoderButton)
"""布局"""
self.sizer0 = wx.BoxSizer (wx.HORIZONTAL )
self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
self.sizer2.Add(encoderButton, 1, wx.EXPAND)
self.sizer2.Add(decoderButton, 1, wx.EXPAND)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.sizer0, 0, wx.EXPAND)
self.sizer.Add(self.contents, 1, wx.EXPAND)
self.sizer.Add(self.coder, 1, wx.EXPAND)
self.sizer.Add(self.sizer2, 0, wx.EXPAND)
self.SetSizer(self.sizer)
self.SetAutoLayout(1)
self.sizer.Fit(self)
"""显示布局"""
self.Show(True)
3.接下来编写各种回调函数,
有时候信息太长,不好一一输进输入框,也懒得复制粘贴,
因此可以写一个函数用以打开文本:
"""Open 函数"""
def OnOpen(self,event):
self.dirname=''
dlg = wx.FileDialog(self,"choose a file",self.dirname,"","*.*",wx.FD_DEFAULT_STYLE )
if dlg.ShowModal()==wx.ID_OK :
self.filename = dlg.GetFilename()
self.dirname = dlg.GetDirectory()
f = open(os.path.join(self.dirname,self.filename),'r')
self.contents.SetValue(f.read())
f.close()
dlg.Destroy()
与之类似,可以编写一个用以保存编码/解码信息的函数.
摩斯电码的点和划有时候看上去好像是字符画,
因此可以写一个设置字体大小、样式和颜色的函数.
"""设置contents字体 """
def OnSelectFont(self, evt):
msg = self.contents.GetValue()
data = wx.FontData()
data.EnableEffects(True)
data.SetColour(self.msgColour) # set colour
data.SetInitialFont(self.msgFont)
dlg = wx.FontDialog(self, data)
if dlg.ShowModal() == wx.ID_OK:
data = dlg.GetFontData()
font = data.GetChosenFont()
colour = data.GetColour()
self.msgFont = font
self.msgColour = colour
self.contents.SetFont(self.msgFont)
self.contents.SetForegroundColour(self.msgColour)
dlg.Destroy()
4.编写解码函数将输入的摩斯电码转为数字和字符:
def Decode(self,event):
Decode_value = CODE.keys()
Decode_key = CODE.values()
Decode_dict = dict(zip(Decode_key,Decode_value))
msg = self.contents.GetValue()
self.coder.Clear()
msg1 = msg.split()
text = []
for str in msg1:
if str in Decode_dict.keys():
text.append(Decode_dict[str])
self.coder.write("%s " % (text))
与之类似,参考可以编码函数编写一个编码函数将输入的字符和数字转为摩斯电码.
python实现的摩斯电码解码\编码器
python实现的摩斯电码解码\编码器的更多相关文章
- 实验吧-密码学-try them all(加salt的密码)、robomunication(摩斯电码)、The Flash-14(闪电侠14集)
try them all(加salt的密码) 首先,要了解什么事加salt的密码. 加salt是一种密码安全保护措施,就是你输入密码,系统随机生成一个salt值,然后对密码+salt进行哈希散列得到加 ...
- [CTF]摩斯电码
摩尔斯电码 -----------转载 https://morse.supfree.net/ 摩尔斯电码定义了包括:英文字母A-Z(无大小写区分)十进制数字0-9,以及"?"&qu ...
- [LeetCode] 804. Unique Morse Code Words 独特的摩斯码单词
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- Javascript实现摩斯码加密解密
原文地址 作者:liaoyu 摩尔斯电码是一种时通时断的信号代码,通过不同的排列顺序来表达不同的英文字母.数字和标点符号,是由美国人萨缪尔·摩尔斯在1836年发明. 每一个字符(字母或数字)对应不同的 ...
- uva 508 - Morse Mismatches(摩斯码)
来自https://blog.csdn.net/su_cicada/article/details/80084529 习题4-6 莫尔斯电码(Morse Mismatches, ACM/ICPC Wo ...
- 摩尔斯电码(Morse Code)Csharp实现
摩尔斯电码,在早期的"生产斗争"生活中,扮演了很重要的角色,作为一种信息编码标准,摩尔斯电码拥有其他编码方案无法超越的长久生命.摩尔斯电码在海事通讯中被作为国际标准一直使用到199 ...
- Python中进行Base64编码和解码
Base64编码 广泛应用于MIME协议,作为电子邮件的传输编码,生成的编码可逆,后一两位可能有“=”,生成的编码都是ascii字符.优点:速度快,ascii字符,肉眼不可理解缺点:编码比较长,非常容 ...
- 算法提高 9-3摩尔斯电码 map
算法提高 9-3摩尔斯电码 时间限制:1.0s 内存限制:256.0MB 问题描述 摩尔斯电码破译.类似于乔林教材第213页的例6.5,要求输入摩尔斯码,返回英文.请不要使用"z ...
- [LeetCode] Unique Morse Code Words 独特的摩斯码单词
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
随机推荐
- Qt解决:Qobject::connect queue arguments of type ‘xxxx’,Make sure ‘xxxx’ is registered using qRegister
解决方法:在调用connect之前,通过 qRegisterMetaType() 注册你connect函数里对象的类型代码如下: typedef QString CustomString;//你自己定 ...
- linux 查看机器内存方法 (free命令)
工作中遇到了统计机器内存的问题.记录一下. free命令可以查看那机器内存. 如下图单位是M 查看man free可以知道,也可以直接从/proc/meminfo文件中读取.
- VS Code .vue文件代码缩进以及格式化代码
首先在应用商店中搜索“Vetur”插件安装,然后进行下面操作: 文件->首选项->设置,然后在右边编辑框输入以下设置: { "prettier.tabWidth": 4 ...
- 【Spark】Spark Streaming 动态更新filter关注的内容
Spark Streaming 动态更新filter关注的内容 spark streaming new thread on driver_百度搜索 (1 封私信)Spark Streaming 动态更 ...
- 经典算法题每日演练——第十一题 Bitmap算法 (转)
http://www.cnblogs.com/huangxincheng/archive/2012/12/06/2804756.html 在所有具有性能优化的数据结构中,我想大家使用最多的就是hash ...
- Masonry应用【美图秀秀首页界面自动布局】
Masonry在此实现时候,并没有比NSLayoutConstraint简单,相反我觉得还不如NSLayoutConstraint. [self.topView mas_makeConstraints ...
- Springboot单元测试(MockBean||SpyBean)
转载:https://blog.csdn.net/maiyikai/article/details/78483423 本来要写springboot集成netty实现的,但是想起来单元测试没总结,那就趁 ...
- 虚拟机Linux下解决ping时出现 unknown host问题
在虚拟机中使用CentOS6.5时,ping www.baidu.com出现报错信息:“ping: unknown hostwww.baidu.com”,虚拟机和物理机网络连接是NAT方式,物理机访问 ...
- IntelliJ IDEA 优化总结
1.修改JVM参数 (IntelliJ IDEA 10.0.1包含以上版本不需要设置) 修改idea.exe.vmoptions配置文件调整以下内容:-Xms256m-Xmx384m-XX:MaxPe ...
- 【代码片段】如何使用CSS来快速定义多彩光标
对于web开发中,我们经常都看得到需要输入内容的组件和元素,比如,textarea,或者可编辑的DIV(contenteditable) ,如果你也曾思考过使用相关方式修改一下光标颜色的,那么这篇技术 ...