-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
311 lines (300 loc) · 7.83 KB
/
index.html
File metadata and controls
311 lines (300 loc) · 7.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<html>
<head>
<script type="text/javascript">
/**
* JavaScript MorseMap
*
* @author Ray Colt <ray_colt@pentagon.mil>
* @copyright Copyright (c) 2015 Ray Colt
* @license Public General License US Army
*
*/
var morseMap = {
" " : "", // SPACE (0b1)
"!" : "101011", // -.-.--
"\"" : "010010", // .-..-.
"$" : "0001001", // ...-..-
"'" : "011110", // .----.
"(" : "10110", // -.--.
")" : "101101", // -.--.-
"," : "110011", // --..--
"-" : "100001", // -....-
"." : "010101", // .-.-.-
"/" : "10010", // -..-.
"0" : "11111", // -----
"1" : "01111", // .----
"2" : "00111", // ..---
"3" : "00011", // ...--
"4" : "00001", // ....-
"5" : "00000", // .....
"6" : "10000", // -....
"7" : "11000", // --...
"8" : "11100", // ---..
"9" : "11110", // ----.
":" : "111000", // ---...
";" : "101010", // -.-.-.
"=" : "10001", // -...-
"?" : "001100", // ..--..
"@" : "011010", // .--.-.
"A" : "01", // .-
"B" : "1000", // -...
"C" : "1010", // -.-.
"D" : "100", // -..
"E" : "0", // .
"F" : "0010", // ..-.
"G" : "110", // --.
"H" : "0000", // ....
"I" : "00", // ..
"J" : "0111", // .---
"K" : "101", // -.-
"L" : "0100", // .-..
"M" : "11", // --
"N" : "10", // -.
"O" : "111", // ---
"P" : "0110", // .--.
"Q" : "1101", // --.-
"R" : "010", // .-.
"S" : "000", // ...
"T" : "1", // -
"U" : "001", // ..-
"V" : "0001", // ...-
"W" : "011", // .--
"X" : "1001", // -..-
"Y" : "1011", // -.--
"Z" : "1100", // --..
"_" : "001101", // ..--.-
"ERR" : "00000000" // ........
};
var reversedMorseMap = array_flip(morseMap);
/**
* Similar to array_flip in php, returns an array in flip order, <br>
* i.e. keys from array become values and values from array become keys. <br>
*
* @param arr
* @return array
*/
function array_flip(arr) {
var key = null;
var tmp_arr = {};
for (key in arr) {
if (arr.hasOwnProperty(key))
tmp_arr[arr[key]] = key;
}
return tmp_arr;
}
/**
* Similar to strtr in php, characters in 'from' will be <br>
* replaced by characters in 'to' in the same <br>
* order character by character.
*
* @param str
* @param from
* @param to
* @return String
*/
function strtr(str, from, to) {
var out = null;
var len = 0;
for (var i = 0, len = str.length; i < len; i++) {
var c = str.charAt(i);
var p = from.indexOf(c);
if (p >= 0) {
if (out == null)
out = str.split("");
out[i] = to.charAt(p);
}
}
return out != null ? out.join("") : str;
}
/**
* Get binary morse code (dit/dah) for a given character
*
* @param character
* @return String
*/
function getBinChar(character) {
return morseMap[character];
}
/**
* Get morse code (dit/dah) for a given character
*
* @param character
* @return String
*/
function getMorse(character) {
return strtr(morseMap[character], "01", ".-");
}
/**
* Get character for given morse code
*
* @param morse
* @return String
*/
function getCharacter(morse) {
return reversedMorseMap[strtr(morse, ".-", "01")];
}
/**
* Get binary morse code for given string
*
* @param str
* @return String
*/
function morse_binary(str) {
var line = "";
for (var i = 0; i < str.length; i++) {
var chr = str.substring(i, i + 1);
line += getBinChar(chr.toUpperCase());
line += " ";
}
return line.trim();
}
/**
* Get morse code for given string
*
* @param str
* @return String
*/
function morse_encode(str) {
var line = "";
for (var i = 0; i < str.length; i++) {
var chr = str.substring(i, i + 1);
line += getMorse(chr.toUpperCase());
line += " ";
}
return line.trim();
}
/**
* Get character string for given morse code
*
* @param str
* @return String
*/
function morse_decode(str) {
var line = "";
var morse = null;
var morsecodes = str.split(" ");
for (morse in morsecodes) {
if (!morsecodes.hasOwnProperty(morse))
line += " ";
var chr = getCharacter(morsecodes[morse]);
line += chr;
}
return line.replace(/\s{2,}/g," ").trim();
}
/**
* (\ /)
* ( . .)
* Get hexadecimal morse code for given string
*
* @param str
* @param modus
* @return String
*/
function bin_morse_hexadecimal(str, modus)
{
var str1, str2;
var a = ["2E ", "2D ", "30 ", "31 "];
if (modus == 0) { str1 = a[0]; str2 = a[1]; };
if (modus == 1) { str1 = a[2]; str2 = a[3]; };
var line = morse_binary(str);
line = line.replace(/ /g, "A");
line = line.replace(/ /g, "K");
line = line.replace(/0/g, str1);
line = line.replace(/1/g, str2);
line = line.replace(/A/g, "20 20 ");
line = line.replace(/K/g, "20 ");
return line.trim();
}
/**
* (\ /)
* ( . .)
* Get txt for given hexadecimal morse code
*
* @param str
* @param modus
* @return string
*/
function hexadecimal_bin_txt(str, modus)
{
var str1, str2;
var a = ["2E", "2D", "30", "31"];
if (modus == 0) { str1 = a[0]; str2 = a[1]; };
if (modus == 1) { str1 = a[2]; str2 = a[3]; };;
var line = str.replace(/ /g,"");
line = line.replace(/2020/g, " ");
line = line.replace(/20/g, " ");
line = line.replace(new RegExp(str1,'g'), "0");
line = line.replace(new RegExp(str2,'g'), "1");
return morse_decode(line.trim());
}
</script>
</head>
<body>
<script type="text/javascript">
function morseForm() {
var modus = document.querySelector('input[name="modus"]:checked').value;
var data = document.getElementById("data").value;
if(modus == "encode")
document.getElementById("data").value = morse_encode(data);
if(modus == "decode")
document.getElementById("data").value = morse_decode(data);
if(modus == "binary_encode")
document.getElementById("data").value = morse_binary(data);
if(modus == "binary_decode")
document.getElementById("data").value = morse_decode(data);
if(modus == "encode_hex")
document.getElementById("data").value = bin_morse_hexadecimal(data, 0);
if(modus == "encode_hex_bin")
document.getElementById("data").value = bin_morse_hexadecimal(data, 1);
if(modus == "decode_hex")
document.getElementById("data").value = hexadecimal_bin_txt(data, 0);
if(modus == "decode_hex_bin")
document.getElementById("data").value = hexadecimal_bin_txt(data, 1);
}
</script>
<table width="100%" border="0" cellspacing="2" cellpadding="2">
<tr>
<td align="center"><b>Morse</b></td>
</tr>
<tr>
<td align="center"><textarea id="data" name="data" cols="50" rows="15">ABC DEFGHIJKLMNOPQRSTUVWXYZ 12 34567890 !$'(),-./ :; =?@"</textarea></td>
</tr>
<tr>
<td align="center"><table width="333" border="0">
<tr>
<td align="center"><label>
<input type="radio" id="modus" name="modus" value="encode" checked="checked"/>
Encode</label></td>
<td align="center"><label>
<input type="radio" id="modus" name="modus" value="decode" />
Decode</label></td>
<td align="center"><label>
<input type="radio" id="modus" name="modus" value="binary_encode" />
Binary Encode</label></td>
<td align="center"><label>
<input type="radio" id="modus" name="modus" value="binary_decode" />
Binary Decode</label></td>
</tr>
<tr>
<td align="center"><label>
<input type="radio" id="modus" name="modus" value="encode_hex" />
Encode Hex</label></td>
<td align="center"><label>
<input type="radio" id="modus" name="modus" value="decode_hex" />
Decode Hex</label></td>
<td align="center"><label>
<input type="radio" id="modus" name="modus" value="encode_hex_bin" />
Encode Hex Bin</label></td>
<td align="center"><label>
<input type="radio" id="modus" name="modus" value="decode_hex_bin" />
Decode Hex Bin</label></td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center"><input type="submit" onClick="morseForm()" /></td>
</tr>
</table>
</body>
</html>