English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Traductor de código Morse en Python

En la criptografía se utiliza el traductor de código Morse. Fue nombrado por Samuel FB Morse. A través de esta técnica, convertimos el mensaje en una serie de puntos, comas, “-”,“ /”。

Esta técnica es muy simple. Cada letra inglesa representa una serie de “.”, “,”, “ /”,“-”。Solo ciframos el mensaje del mensaje a símbolos y luego lo desciframos del símbolo al inglés.

El diccionario es el siguiente

'A': '.',-MORSE_CODE_DICT = { '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':'-----, , :'--.. .--, .:'.-.-.-,
'?':'..--..', ./:'-.. .-. .-:'-....-,
(:'-.--. .):'-.--.-}

Ejemplo

El mensaje es PYTHON-PROGRAM
El resultado es .--. -.-- - .... --- -.  -....- .--. .-. --- --. .-. .- --

Algoritmo

Cifrado

consecutive spaces we will add another space to our variable containing the decoded string.1: Dado una cadena, primero extraemos cada letra de la palabra y la coincidimos con el diccionario de código Morse, luego consideramos el código correspondiente a la letra.
consecutive spaces we will add another space to our variable containing the decoded string.2: El siguiente paso es almacenar el código en una variable. Y debemos seguir que se debe mantener un espacio entre cada código Morse.
consecutive spaces we will add another space to our variable containing the decoded string.3: Two spaces should be maintained in between every word.

: Two spaces should be maintained in between every word.

consecutive spaces we will add another space to our variable containing the decoded string.1: 解密
consecutive spaces we will add another space to our variable containing the decoded string.2: First we add a space at the end of the string.
consecutive spaces we will add another space to our variable containing the decoded string.3: Now we traverse each letter of the message until space is not encountered.
consecutive spaces we will add another space to our variable containing the decoded string.4: When we get space then check with Morse Code Dictionary and store in a variable. 2 : When get
consecutive spaces we will add another space to our variable containing the decoded string.5: Step

: When get last space of the message that means this is the last letter of Morse Code Generator.

范例程式码 -*- #-8 -*-
@author: Satyajit
coding: utf  2 11:21:31 2018
Created on Tue Oct
@author: Satyajit
"""
# Dictionary representing the morse code chart-MORSE_CODE_DICT = { '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':'-----, , :'--.. .--, .:'.-.-.-,
   '?':'..--..', ./:'-.. .-. .-:'-....-,
   (:'-.--. .):'-.--.-'
}
def encryption(message):
   my_cipher = ''
   for myletter in message:
      if myletter != ' ':
         my_cipher += MORSE_CODE_DICT[myletter] + ' '
      else:
         my_cipher += ' '
      return my_cipher
# Esta función se utiliza para desencriptar
# Código Morse a inglés
def decryption(message):
   message += ' '
   decipher = ''
   mycitext = ''
   for myletter in message:
      # verifica por espacio
      if (myletter != ' '):
         i = 0
         mycitext += myletter
      else:
         i += 1
         if i == 2 :
            decipher += ' '
         else:
            decipher += list(MORSE_CODE_DICT.keys())[list(MORSE_CODE_DICT
            .values()).index(mycitext)
            mycitext = ''
   return decipher
def main():
   my_message = "PYTHON"-PROGRAM"
   output = encryption(my_message.upper())
   print (output)
   my_message = "."--. -.-- - .... --- -.  -....- .--. .-. --- --. .-. .- -- "
   output = decryption(my_message)
   print (output)
# Ejecuta la función principal
if __name__ == '__main__':
	main()

Resultado de salida

.--. -.-- - .... --- -.  -....- .--. .-. --- --. .-. .- --
PYTHON-PROGRAM