摩斯代码在线html,html隐写术,使用摩尔兹电码/莫尔兹电码存储信息 水波纹样式 Morse code...

本文介绍了一个用JavaScript编写的工具库,用于实现字符串到摩尔斯码的转换(parse)和摩尔斯码到字符串的解码(decode)。它包括字典存储摩尔斯码映射,错误检查功能,以及针对字母、数字和特殊字符的处理。
摘要由CSDN通过智能技术生成

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(){};//使用字典存储摩斯码对照关系

functionDictionary() {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 intempData) {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 invalues) {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= /\/g,

r_find= /^\$/;//store datas mapping

mcode.mdatas = (function(){var dictionaryDS = newDictionary();//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

'.-.-.-','---...','--..--','-.-.-.','..--..','-...-','.----.','-..-.','-.-.--','-....-','..--.-','.-..-.','-.--.','-.--.-','...-..-','.-...','.--.-.','.-.-.']

);returndictionaryDS;

}());//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

item=a_temp[i];if(item !== '') {//IE无法通过下标来索引字符串

if(!item[0]){

item= item.split('');

}for(var j=0; j

a_value[count++] =item[j];

}

}//当前字符串为形式,提取AS字符

if(i !== a_temp.length - 1){

a_value[count++] = a_special[i].match(r_find)[1];

}

}//将解析数组形式的用户输入值

for(var i=0; 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] + ' ';

}

}returnutils.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

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+= '';

}else{

result+=c_result;

}

}

}

}returnresult;

}returnmcode;

}(this));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值