摩爾斯電碼是一種時通時斷的訊號代碼,通過不同的排列順序來表達不同的英文字母、數字和標點符號,是由美國人薩繆爾·摩爾斯在1836年發明。
每一個字元(字母或數字)對應不同的序列(由點和劃組成)。
一般來說,任何一種能把書面字元用可變長度的訊號表示的編碼方式都可以稱為摩爾斯電碼。
但現在這一術語只用來特指兩種表示英語字母和符號的摩爾斯電碼:美式摩爾斯電碼和國際摩爾斯電碼。下面內容僅針對國際摩爾斯電碼。
字母、數字、標點、特殊字元與摩斯碼對照表
字母
字母 摩斯碼 字母 摩斯碼 字母 摩斯碼 字母 摩斯碼 字母 摩斯碼
--- ---- --- --- --- ---- --- --- --- ----
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 -----
標點符號
字元 摩斯碼 字元 摩斯碼 字元 摩斯碼 字元 摩斯碼 字元 摩斯碼
--- ---- --- --- --- ---- --- --- --- ----
. .-.-.- : ---... , --..-- ; -.-.-. ? ..--..
= -...- ' .----. / -..-. ! -.-.-- - -....-
_ ..--.- " .-..-. ( -.--. ) -.--.- $ ...-..-
& .-... @ .--.-. + .-.-.
特殊字元
有一些特殊的字元來代表特定的含義,比如SOS(緊急求救),這樣可以通過更少的摩斯碼來傳遞資訊。
字元 摩斯碼 字元 摩斯碼
--- --- --- ---
AA,換行 .-.- AR,訊息結束 .-.-.
AS,等待 .-... BK,中斷 -...-.-
BT,新的一頁 -...- CT,開始拷貝 -.-.-
SK,結束傳遞 ...-.- SOS,緊急求助 ...---...
除了上面的字元,也可以通過聲音或光線亮度等媒介來傳遞摩斯密碼資訊,以聲音為例,嘀代表(短聲),噠代表(長聲)。
摩斯碼相關事件
- 前段時間被ISIS處決的日本人質後藤,在行刑過程中他的眨眼行為被摩斯電碼專家解讀為通過眨眼傳遞資訊,翻譯成文字為“不要救我,放棄我!”
- 在1912年的泰坦尼克號遊輪沉沒前,也曾發送SOS的求救訊號。
Javascript實現摩斯碼加密解析
- 使用字典來實現摩斯碼與字元的對應關係,該字典也是基於對象來實現
- 低版本的IE無法通過下標來索引字串進行遍曆
- IE在str.split(regExp) 中存在一個bug
線上示範
完整代碼:
/** @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 libraryvar 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));