Python 中的摩尔斯电码翻译器

2025 年 1 月 11 日 | 阅读 12 分钟

文本消息可以通过摩尔斯电码的方式进行通信,通过输入一系列电脉冲,通常表示为短脉冲(称为“点”)和长脉冲(称为“划”)。 塞缪尔·F·B·莫尔斯(Samuel F. B. Morse)在 1840 年代发明了这种代码,用于他的电报创新,这是第一个成功使用电磁进行长距离通信的设备。 早期的电报员会按键来传达一系列字符,接收电报员可以从纸带上读取。 这位电报员通常在火车站,通过数英里的电报线连接到其他人(后来的操作员学会了仅通过听来读取信号)。 旧的格式使用一个点(或者,对于电报员来说,听起来像“dit”)表示按键按下,然后跟着下一个字母,而划表示快速连续两次按下按键(“dah”或“dit-dit”)。 划、点或任何其他组合都作为每个字符的文本等价物。

在 19 世纪末出现的国际摩尔斯电码或大陆电码中,随着新的按键技术变得普遍,用点和划以略有不同的方式表示某些字母。 但即使到 1960 年代,美国摩尔斯电码在美国仍在使用。 摩尔斯电码的起源是许多传说的对象。 据说塞缪尔·莫尔斯去了一家印刷厂,那里有他数出印刷机每种字母的打印类型数量。 之后,他将这些数字解释为对每种字母在普通英语文本中相对频率的大致估计。 他设计了摩尔斯电码,以便最短的符号对应于使用最多的字母。 因此,英语中最常用的字母 E 和 T 分别用一个点或一个划表示。 数字、标点符号和使用频率最低的字母,如 J 和 Y,则被赋予了更长、更复杂的表示。

当存在干扰、噪音或衰减时,摩尔斯电码提供了一种缓慢但可靠的方式来发送和接收无线文本消息。 这主要是因为其简单的二进制编码(按键按下或抬起)允许非常小的带宽。 人类耳朵和大脑作为数字接收设备的功能也令人惊叹。 业余无线电操作员、固定电话电报员和军事无线电操作员在现代仍然偶尔使用摩尔斯电码。

尽管摩尔斯电码专门为电报而设计,但人们也发现了其额外的用途。 随着 1890 年代无线电报设备的发明,现在可以通过无线电波传输编码通信。 国际摩尔斯电码在一些纯视觉媒介中也有着悠久的历史。 自 1867 年以来,船只开始使用船上的闪光灯相互发送信号。 杰里迈亚·邓顿少校(Jeremiah Denton Jr.)的案例也是如此。 他是一名被俘的美国军方飞行员(后来成为参议员),在越南。 在一次北越电视露面时,邓顿被迫宣称他的俘虏对他很好。 然而,他的眼睛却揭示了不同的故事。 他以摩尔斯电码的形式闪烁,代表“酷刑”一词。 该代码在 20 世纪后半叶基本被淘汰。 自 1995 年以来,美国海岸警卫队不再将其用于官方目的,而现代船舶则更依赖卫星通信设备。 然而,美国海军仍在培训情报专业人员来学习代码。 国际摩尔斯电码保护协会(International Morse Code Preservation Society)是一个由数千名成员组成的全球性业余无线电操作员联盟,也是支持它的另一个组织。 因此,即使点和划的时代可能已经结束,摩尔斯电码仍然在使用。 不需要紧急信号。

它是通过敲击所需数量的点和划,然后是适当的停顿来激活的。 词与词之间有比词内更大的停顿。 使用摩尔斯电码发送的词或数字取决于停顿的长度和点和划的数量。 学习摩尔斯电码需要熟悉这些符号以及每个点和划之间的间隔。 划是长横线,类似于连字符,而点是简单的句点。

摩尔斯电码在其最简单的形式中,是一种早期的通信系统,使用点、划或长短音来对应拉丁字母表中的每个字母。 通常,这些消息通过灯光信号或电报(也称为直键)传输。

现在有许多不同类型的摩尔斯电码,包括适用于也使用拉丁字母的语言的国际摩尔斯电码、日文版、Wabun 代码以及韩文摩尔斯电码 SKATS。 在远距离,消息使用摩尔斯电码传输。 可以使用光或脉冲来传输摩尔斯电码消息。 在塞缪尔·莫尔斯时代,最常使用电报来传送脉冲消息。 直键有时也称为电报,它根据使用电报键“敲击”出的消息发送电脉冲。 之后,电报员会根据消息中的单词拼写方式,通过一系列点击来键入消息。 另一端的电报员会听到点击声,然后将其转换回语音。

摩尔斯电码(也称为点和划)将字母表中的每个字母转换为短信号和长信号。 划的脉冲长度相当于三个点。 当摩尔斯电码被翻译成无线电使用时,点和划最终被称为“dit”和“dah”,这是根据无线电脉冲的声音得名的。

纵观历史,世界各地的文明都试图创造长距离通信技术。 他们使用旗帜、灯光、音乐和烟雾信号进行交流。 这些通信技术被称为旗语系统(Semaphore systems)。

代码

输出

Go on selecting any one of the options
To initialize the morse code dictionary.
To enter the string you want to encode/cipher using the morse code technique.
To encode the input string.
To print the encoded string.
To enter the string you want to decode/decipher using the morse code technique.
To decode the input string.
To print the decoded string.
To exit from the code execution.
0
For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
To initialize the morse code dictionary.
To enter the string you want to encode/cipher using the morse code technique.
To encode the input string.
To print the encoded string.
To enter the string you want to decode/decipher using the morse code technique.
To decode the input string.
To print the decoded string.
To exit from the code execution.
1
Enter the message that you want to encode using Morse code cipher::
HELLO MY NAME IS SAMUEL
For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
To initialize the morse code dictionary.
To enter the string you want to encode/cipher using the morse code technique.
To encode the input string.
To print the encoded string.
To enter the string you want to decode/decipher using the morse code technique.
To decode the input string.
To print the decoded string.
To exit from the code execution.
2
Input string encoded successfully
For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
To initialize the morse code dictionary.
To enter the string you want to encode/cipher using the morse code technique.
To encode the input string.
To print the encoded string.
To enter the string you want to decode/decipher using the morse code technique.
To decode the input string.
To print the decoded string.
To exit from the code execution.
3
Encoded message is: .... . .-.. .-.. --- -- -.-- -. .- -- . .. ... ... .- -- ..- . .-..
For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
To initialize the morse code dictionary.
To enter the string you want to encode/cipher using the morse code technique.
To encode the input string.
To print the encoded string.
To enter the string you want to decode/decipher using the morse code technique.
To decode the input string.
To print the decoded string.
To exit from the code execution.
4
Enter the message that you want to decode using Morse code cipher::
.... . .-.. .-.. --- -- -.-- -. .- -- . .. ... ... .- -- ..- . .-..
For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
To initialize the morse code dictionary.
To enter the string you want to encode/cipher using the morse code technique.
To encode the input string.
To print the encoded string.
To enter the string you want to decode/decipher using the morse code technique.
To decode the input string.
To print the decoded string.
To exit from the code execution.
5
Input string decoded successfully
For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
To initialize the morse code dictionary.
To enter the string you want to encode/cipher using the morse code technique.
To encode the input string.
To print the encoded string.
To enter the string you want to decode/decipher using the morse code technique.
To decode the input string.
To print the decoded string.
To exit from the code execution.
6
The decoded message is: HELLO MY NAME IS SAMUEL
For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
To initialize the morse code dictionary.
To enter the string you want to encode/cipher using the morse code technique.
To encode the input string.
To print the encoded string.
To enter the string you want to decode/decipher using the morse code technique.
To decode the input string.
To print the decoded string.
To exit from the code execution.
7

输出解释

在主函数中编写的代码中,创建了上述类的对象,并使用该对象调用类中的各种函数。 主函数为用户提供一个菜单,其中包含各种选项供用户选择。 向用户显示的各种选项包括:初始化摩尔斯电码字典,输入要使用摩尔斯电码技术编码/加密的字符串,编码输入字符串,打印编码字符串,输入要使用摩尔斯电码技术解码/解密的字符串,解码输入字符串,打印解码字符串。在打印了所有这些选项后,用户可以从中选择任何一个,并且根据用户选择的选项,会调用相应的函数并执行该函数内的步骤。如果调用的函数需要一些输入,则代码会要求用户提供该特定输入并继续执行代码。菜单的显示会一直持续,直到用户选择最后一个选项,即退出代码选项。导致代码执行停止的另一个选项或条件是代码执行中遇到的任何异常。

摩尔斯电码的优点

  • 与其他的传统语音通信工具相比,摩尔斯电码可以通过业余无线电发射器以更低的功率和更窄的带宽发送,使其成为在紧急情况下进行通信的有效方式。 此外,它也是秘密通信的有用工具,因为只有精通代码的人才能理解这些消息。
  • 学习摩尔斯电码虽然不像过去那样有许多职业用途,但可以为您提供高度的智力丰富。 通过挑战自己破解代码,您可以进行一项脑力锻炼,这可能会提高您学习其他有用能力的能力。
  • 个人成就:学习新事物,无论其重要性如何,都可以提高您的自信心并给您带来成就感。 因此,掌握摩尔斯电码可能会提升您的整体自信心,这可能对您生活的其他方面有所帮助。
  • 鉴于现代技术的进步,摩尔斯电码不再是一种可靠的解决方案,因为它很容易被破译。 更好的技术可以提供更好的信息传输途径。 摩尔斯电码的用途历史上一直在各种场合使用,包括业余无线电和航空领域。 摩尔斯电码正在通过辅助技术帮助有听力或运动障碍的人进行交流。

结论

因此,在本文中,我们了解了如何在 Python 中实现摩尔斯电码编码器和解码器。 在本文中,我们编写了一个 Python 代码,它既是摩尔斯电码的编码器,也是解码器。 该代码提供了多种选项,我们可以从中选择合适的选项作为输入,然后代码将执行该特定操作。