原文地址 作者:liaoyu

摩尔斯电码是一种时通时断的信号代码,通过不同的排列顺序来表达不同的英文字母、数字和标点符号,是由美国人萨缪尔·摩尔斯在1836年发明。

每一个字符(字母或数字)对应不同的序列(由点和划组成)。

一般来说,任何一种能把书面字符用可变长度的信号表示的编码方式都可以称为摩尔斯电码。

但现在这一术语只用来特指两种表示英语字母和符号的摩尔斯电码:美式摩尔斯电码和国际摩尔斯电码。下面内容仅针对国际摩尔斯电码。


字母、数字、标点、特殊字符与摩斯码对照表

除了上面的字符,也可以通过声音或光线亮度等媒介来传递摩斯密码信息,以声音为例,嘀代表(短声),哒代表(长声)。

摩斯码相关事件

  1. 前段时间被ISIS处决的日本人质后藤,在行刑过程中他的眨眼行为被摩斯电码专家解读为通过眨眼传递信息,翻译成文字为“不要救我,放弃我!”
  2. 在1912年的泰坦尼克号游轮沉没前,也曾发送SOS的求救信号。

Javascript实现摩斯码加密解析

  1. 使用字典来实现摩斯码与字符的对应关系,该字典也是基于对象来实现
  2. 低版本的IE无法通过下标来索引字符串进行遍历
  3. IE在str.split(regExp) 中存在一个bug
var str = 'abb' ;
var r = /b/g ;
alert(str.split('b')); // chrome下提示 a,, IE8下提示 a,,
alert(str.split(r)); // chrome下提示 a,, IE8下提示 a

在线演示

完整代码:

/*
* @author liaoyu
* @created 2015-03-14
*/ var utils = utils || {}; utils.isArray = function(value) {
return Object.prototype.toString.apply(value) === '[object Array]';
} utils.trim = function(value) {
return value.trim ? value.trim() : value.replace(/^\s+|\s+$|/g,'');
} // 解决IE不兼容console问题
var console = console || {};
console.log = console.log || function(){};
console.error = console.error || function(){}; // 使用字典存储摩斯码对照关系
function Dictionary() {
this.datasource = {};
this.rdatasource = {};
} Dictionary.prototype.add = function(keys, values) {
if(typeof keys === 'undefined' || typeof values === 'undefined') {
console.error('Illegal arguments');
return ;
}
if(utils.isArray(keys) && utils.isArray(values)) {
if(keys.length != values.length) {
console.error('keys length not equals values length');
return ;
}
for(var i = 0; i < keys.length; i++) {
this.datasource[keys[i]] = values[i];
}
return ;
}
this.datasource[keys] = values;
} Dictionary.prototype.reversal = function(){
var tempData = this.datasource;
for(var i in tempData) {
if(tempData.hasOwnProperty(i)) {
this.rdatasource[tempData[i]] = i;
}
}
} Dictionary.prototype.showAll = function(values) {
var count = 0;
console.log('-----------morse code mapping-----------');
for(var i in values) {
if(values.hasOwnProperty(i)) {
count++;
console.log(i + '\t > ' + values[i]);
}
}
console.log('total count: ' + count);
} // morse code library
var morse = (function(global){
var mcode = {},
r_special = /\<\w+\>/g,
r_find = /^\<(\w+)\>$/; // store datas mapping
mcode.mdatas = (function(){
var dictionaryDS = new Dictionary();
// initial mappping
dictionaryDS.add(
[
'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',
'AA','AR','AS','BK','BT','CT','SK','SOS',
'.',':',',',';','?','=',"'",'/','!','-','_','"','(',')','$','&','@','+'
],
[
// letter
'.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..',
// number
'.----','..---','...--','....-','.....','-....','--...','---..','----.','-----',
// special charactor
'.-.-','.-.-.','.-...','-...-.-','-...-','-.-.-','...-.-','...---...',
// punctuation
'.-.-.-','---...','--..--','-.-.-.','..--..','-...-','.----.','-..-.','-.-.--','-....-','..--.-','.-..-.','-.--.','-.--.-','...-..-','.-...','.--.-.','.-.-.'
]
);
return dictionaryDS;
}()); // error flag
mcode.error_flag = false; // 将字符串转换为摩斯码
mcode.parse = function(values) {
// console.log('input: ' + values);
this.error_flag = false; var _datasource = this.mdatas.datasource,
item = '',
a_special = [],
a_temp = [],
a_value = [],
count = 0,
result = '';
values = values.toUpperCase();
a_special = values.match(r_special);
a_temp = values.split(r_special); // 将用户输入的字符串转换成数组
for(var i=0; i<a_temp.length; i++) {
item = a_temp[i];
if(item !== '') {
// IE无法通过下标来索引字符串
if(!item[0]){
item = item.split('');
}
for(var j=0; j<item.length; j++) {
a_value[count++] = item[j];
}
} // 当前字符串为<AS>形式,提取AS字符
if(i !== a_temp.length - 1){
a_value[count++] = a_special[i].match(r_find)[1];
}
} // 将解析数组形式的用户输入值
for(var i=0; i<a_value.length; i++) {
item = a_value[i]; if(item === ' ') {
result += '/ ';
} else if(typeof _datasource[item] === 'undefined') {
this.error_flag = true;
// console.error('Invalid characters in input.')
result += '? ';
}else {
result += _datasource[item] + ' ';
}
}
return utils.trim(result);
} //将摩斯码转换成字符串
mcode.decode = function(values) {
// console.log('input: ' + values);
this.error_flag = false; this.mdatas.reversal();
var _rdatasource = this.mdatas.rdatasource,
a_input = values.split(' '),
result = '',
item = '',
c_result = ''; for(var i=0; i<a_input.length; i++) {
item = a_input[i];
if(item === '/') {
result += ' ';
}else {
c_result = _rdatasource[item];
if(typeof c_result === 'undefined') {
this.error_flag = true;
// console.error('Invalid characters in input.')
result += '?';
} else {
if(c_result.length > 1){
result += '<' + c_result + '>';
} else {
result += c_result;
}
}
}
}
return result;
} return mcode;
}(this));

参考资料链接

Morse Code Wiki

摩尔斯电码中文WIKI

International Morse Code对照表

摩斯码转换实现参考

Javascript实现摩斯码加密解密的更多相关文章

  1. uva 508 - Morse Mismatches(摩斯码)

    来自https://blog.csdn.net/su_cicada/article/details/80084529 习题4-6 莫尔斯电码(Morse Mismatches, ACM/ICPC Wo ...

  2. 兼容javascript和C#的RSA加密解密算法,对web提交的数据进行加密传输

    Web应用中往往涉及到敏感的数据,由于HTTP协议以明文的形式与服务器进行交互,因此可以通过截获请求的数据包进行分析来盗取有用的信息.虽然https可以对传输的数据进行加密,但是必须要申请证书(一般都 ...

  3. [LeetCode] Unique Morse Code Words 独特的摩斯码单词

    International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...

  4. [LeetCode] 804. Unique Morse Code Words 独特的摩斯码单词

    International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...

  5. 生成二维码 加密解密类 TABLE转换成实体、TABLE转换成实体集合(可转换成对象和值类型) COOKIE帮助类 数据类型转换 截取字符串 根据IP获取地点 生成随机字符 UNIX时间转换为DATETIME\DATETIME转换为UNIXTIME 是否包含中文 生成秘钥方式之一 计算某一年 某一周 的起始时间和结束时间

    生成二维码 /// <summary>/// 生成二维码/// </summary>public static class QRcodeUtils{private static ...

  6. keyring源码加密解密函数分析

    Encrypt the page data contents. Page type can't be FIL_PAGE_ENCRYPTED, FIL_PAGE_COMPRESSED_AND_ENCRY ...

  7. python-摩斯码转换

    意义:简单实现摩斯码的破译和生成 代码: #-*- coding: UTF-8 -*- ' __date__ = '2016/2/2' import pprint import re chars = ...

  8. JavaScript加密解密7种方法总结分析

    原文地址:http://wenku.baidu.com/view/9048edee9e31433239689357.html 本文一共介绍了七种javascript加密方法: 在做网页时(其实是网页木 ...

  9. Java & PHP & Javascript 通用 RSA 加密 解密 (长字符串)

    系统与系统的数据交互中,有些敏感数据是不能直接明文传输的,所以在发送数据之前要进行加密,在接收到数据时进行解密处理:然而由于系统与系统之间的开发语言不同. 本次需求是生成二维码是通过java生成,由p ...

随机推荐

  1. 备份BinLog并压缩 全备份

    Rem Backup Mysql Binlog Rem Backup Yesterday and RAR Rem Backup every day 00:01 begin backup yesterd ...

  2. IOS-用动画组制作花瓣掉落效果(另附iOS动画图表)

    重要的两个方法:1.动画的数组:animations 2.启动的时间 beginTime 注意:动画组设置了持续时间(duration)可能会导致动画组里面的持续时间不管用 代码如下: #import ...

  3. Golang,用map写个单词统计器

    Golang中也有实用的泛型编程模板.如map.据Go官方团队称,其实现为Hash表,而非类似cpp或Java的红黑树.所以理论上速度更能快上几个等级(Hash与红黑树的效率对比可以看我的文章C++中 ...

  4. C# 编码约定

    参考自 MSDN     https://msdn.microsoft.com/zh-cn/library/ff926074.aspx , 只摘要个人觉得有用部分 命名约定 在不包括 using 指令 ...

  5. JavaScript对象应用-字符串和图片对象

    1.1 应用 String对象截取特定文字   利用String 对象的charAt() 和 substring() 方法等,截取特定文字或字段文字显示在页面上 <html> <he ...

  6. 用Python进行语音信号处理

    1.语音信号处理之时域分析-音高追踪及其Python实现 2.语音信号处理之时域分析-音高及其Python实现 参考: 1.NumPy

  7. [PR & ML 3] [Introduction] Probability Theory

    虽然学过Machine Learning和Probability今天看着一part的时候还是感觉挺有趣,听惊呆的,尤其是Bayesian Approach.奇怪发中文的笔记就很多人看,英文就没有了,其 ...

  8. sea.js说明文档

    Sea.js 手册与文档 首页 | 索引 目录 模块定义 define id dependencies factory exports require require.async require.re ...

  9. selenium自动化测试(1):环境搭建

    Selenium是一款优秀的WEB自动化测试工具,它功能强大,易于使用,支持多种平台.多种浏览器和多种开发语言.这里介绍使用python+selenium进行自动化测试的一些基础知识. 在Window ...

  10. csdn博客刷点击率代码

    此文为转载,亲测有效. import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; impo ...