-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
456 lines (454 loc) · 23.8 KB
/
index.html
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
<!DOCTYPE html>
<html>
<head>
<style>
input.decimal {width: 110px; letter-spacing: 2px; text-align: right; font-family: monospace}
input.editable, textarea.editable {border: 1px solid #003cff}
input:invalid {border: 2px solid red}
input.binary {width: 300px; letter-spacing: 2px; text-align: right; font-family: monospace}
input.hexa {width: 75px; letter-spacing: 2px; text-align: right; font-family: monospace}
input.num {width: 35px; letter-spacing: 2px; text-align: right; font-family: monospace}
textarea.txtflat {width: 580px; height: 50px; letter-spacing: 1px; text-align: left; font-family: monospace; resize: none}
textarea.encoded {width: 580px; height: 120px; letter-spacing: 1px; text-align: left; font-family: monospace; resize: none}
body {background-color:hsla(120,100%,75%,0.1); font-family: monospace}
</style>
<script>
/* eslint-disable no-bitwise */
/* eslint-disable no-invalid-this */
let
binfields = null,
decfields = null,
hexfields = null,
numfields = null,
textfields = null,
utf8fields = null,
utf16fields = null;
const
// -------------------------------------
// binary fillers per integer type (8, 16, 32 bits)
// eslint-disable-next-line no-unused-vars
[ BIN_FILLER_8, BIN_FILLER_16, BIN_FILLER_32 ] = [ `00000000`, `0000000000000000`, `00000000000000000000000000000000` ],
// hexadecimal fillers per integer type (8, 16, 32 bits)
[ HEX_FILLER_8, HEX_FILLER_16, HEX_FILLER_32 ] = [ `00`, `0000`, `00000000` ],
// -------------------------------------
base2base = (num, src, tgt, filler) => {
const
n = (parseInt(num, src) >>> 0).toString(tgt).toUpperCase(),
[ r, l ] = filler ? [ `${ filler }${ n }`, filler.length ] : [ n, n.length ];
return r.substring(r.length - l);
},
// -------------------------------------
// encodes a string in utf8 code units
utf8Encode = s => {
const
// spread to utf16 code points
sp = [ ...s ],
// results buffer
bf = [];
// encode code points to utf8 code units
for (let i = 0; i < sp.length; i++) {
const
// extract full code point
cp = sp[i].codePointAt(0);
// encode
switch (true) {
// U+0000 U+007F
case cp > 0x0000 && cp < 0x007f :
// 0xxxxxxx
bf.push(cp & 0b01111111);
break;
// U+0080 U+07FF
case cp > 0x0080 && cp < 0x07ff :
// 110xxxxx 10xxxxxx
bf.push(0b11000000 | cp >> 6 & 0b00011111, 0b10000000 | cp & 0b00111111);
break;
// U+0800 U+FFFF
case cp > 0x0800 && cp < 0xffff :
// 1110xxxx 10xxxxxx 10xxxxxx
bf.push(0b11100000 | cp >> 12 & 0b00001111, 0b10000000 | cp >> 6 & 0b00111111, 0b10000000 | cp & 0b00111111);
break;
// U+10000 U+10FFFF
case cp > 0x10000 && cp < 0x10ffff :
// 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
bf.push(0b11110000 | cp >> 18 & 0b00000111, 0b10000000 | cp >> 12 & 0b00111111, 0b10000000 | cp >> 6 & 0b00111111, 0b10000000 | cp & 0b00111111);
break;
// monkaHmm ...
default :
break;
}
}
// return buffer
return bf;
},
// -------------------------------------
// encodes a string in utf16 code units
utf16Encode = s => {
const
// results buffer
bf = [];
// loop on utf16 code units using builtins
for (let i = 0; i < s.length; i++)
bf.push(s.charCodeAt(i));
// return buffer
return bf;
},
// -------------------------------------
applyFilter = filter => {
if (filter(this.value)) {
Object.assign(this, {
oldValue: this.value,
oldSelectionStart: this.selectionStart,
oldSelectionEnd: this.selectionEnd
});
} else if (Object.prototype.hasOwnProperty.call(this, `oldValue`)) {
this.value = this.oldValue;
this.setSelectionRange(this.oldSelectionStart, this.oldSelectionEnd);
} else {
this.value = ``;
}
},
// -------------------------------------
// Restricts input for the given text field to the given filter
setFilter = (fld, filter) => [ `input`, `keydown`, `keyup`, `mousedown`, `mouseup`, `select`, `contextmenu`, `drop` ]
.forEach(s => fld.addEventListener(s, e => applyFilter.call(e.target, filter))),
// -------------------------------------
updatefields = (i, f) => {
if (f === `t`) {
// text value changes, update code points utf8 and utf16
const
// store code points
codePts = [ ...textfields[i].value ],
// store utf8 code units
utf8 = Uint8Array.from(utf8Encode(textfields[i].value)),
// store utf16 code units
utf16 = Uint16Array.from(utf16Encode(textfields[i].value));
// format
textfields[i + 1].value =
codePts
.reduce((r, x) => {
// eslint-disable-next-line newline-per-chained-call
r.push(`U+${ x.codePointAt(0).toString(16).padStart(4, `0000`) }`);
return r;
}, [])
.join(`,`);
utf8fields[i].value =
utf8
.reduce((r, x) => {
r.push(`\\x${ base2base(x, 10, 16, HEX_FILLER_8).toLowerCase() }`);
return r;
}, [])
.join(``);
utf16fields[i].value =
utf16
.reduce((r, x) => {
r.push(`\\u${ base2base(x, 10, 16, HEX_FILLER_16).toLowerCase() }`);
return r;
}, [])
.join(``);
return;
} else if (f === `b`) {
// binary value changes, update decimal and hexa
decfields[i].value = base2base(binfields[i].value, 2, 10, null);
hexfields[i].value = base2base(binfields[i].value, 2, 16, HEX_FILLER_32);
} else if (f === `d`) {
// decimal value changes, update binary and hexa
binfields[i].value = base2base(decfields[i].value, 10, 2, BIN_FILLER_32);
hexfields[i].value = base2base(decfields[i].value, 10, 16, HEX_FILLER_32);
} else if (f === `h`) {
// hexa value changes, update decimal and binary
decfields[i].value = base2base(hexfields[i].value, 16, 10, null);
binfields[i].value = base2base(hexfields[i].value, 16, 2, BIN_FILLER_32);
} else if (f === `s`) {
const
t = 1 ^ i % 2;
// shift values change
if (i < 9) {
binfields[i].value = base2base(decfields[t].value << numfields[i].value, 10, 2, BIN_FILLER_32);
hexfields[i].value = base2base(decfields[t].value << numfields[i].value, 10, 16, HEX_FILLER_32);
} else if (i < 11) {
binfields[i].value = base2base(decfields[t].value >>> numfields[i].value, 10, 2, BIN_FILLER_32);
hexfields[i].value = base2base(decfields[t].value >>> numfields[i].value, 10, 16, HEX_FILLER_32);
} else {
binfields[i].value = base2base(decfields[t].value >> numfields[i].value, 10, 2, BIN_FILLER_32);
hexfields[i].value = base2base(decfields[t].value >> numfields[i].value, 10, 16, HEX_FILLER_32);
}
decfields[i].value = base2base(binfields[i].value, 2, 10, null);
return;
}
if (i <= 1 && f !== `t`) {
// update operations results
[ decfields[0].value & decfields[1].value, // 1 AND 2
decfields[0].value | decfields[1].value, // 1 OR 2
decfields[0].value ^ decfields[1].value, // 1 XOR 2
~decfields[0].value, // NOT 1
~decfields[1].value, // NOT 2
decfields[0].value << numfields[7].value, // ZFLS 1
decfields[1].value << numfields[8].value, // ZFLS 2
decfields[0].value >>> numfields[9].value, // ZFRS 1
decfields[1].value >>> numfields[10].value, // ZFRS 2
decfields[0].value >> numfields[11].value, // SPRS 1
decfields[1].value >> numfields[12].value // SPRS 2
].forEach((x, j) => {
// update binary
binfields[j + 2].value = base2base(x, 10, 2, BIN_FILLER_32);
// update hexa and decimal
binfields[j + 2]
.onchange();
});
}
},
// -------------------------------------
// eslint-disable-next-line no-unused-vars
initfields = () => {
// -------------------------------------
// initialize fields
textfields =
[ `s1Flat`, `s1CodePtshexa` ]
.map(x => document.getElementById(x));
utf8fields =
[ `s1Utf8hexa` ]
.map(x => document.getElementById(x));
utf16fields =
[ `s1Utf16hexa` ]
.map(x => document.getElementById(x));
// -------------------------------------
// install filters
textfields
.forEach((x, i) => {
x.onkeyup = () => updatefields(i, `t`);
});
// -------------------------------------
// initialize fields
decfields =
[ `n1decimal`, `n2decimal`, `n1ANDn2decimal`, `n1ORn2decimal`, `n1XORn2decimal`, `NOTn1decimal`, `NOTn2decimal`, `ZFLSn1decimal`, `ZFLSn2decimal`, `ZFRSn1decimal`, `ZFRSn2decimal`, `SRSn1decimal`, `SRSn2decimal` ]
.map(x => document.getElementById(x));
binfields =
[ `n1binary`, `n2binary`, `n1ANDn2binary`, `n1ORn2binary`, `n1XORn2binary`, `NOTn1binary`, `NOTn2binary`, `ZFLSn1binary`, `ZFLSn2binary`, `ZFRSn1binary`, `ZFRSn2binary`, `SRSn1binary`, `SRSn2binary` ]
.map(x => document.getElementById(x));
hexfields =
[ `n1hexa`, `n2hexa`, `n1ANDn2hexa`, `n1ORn2hexa`, `n1XORn2hexa`, `NOTn1hexa`, `NOTn2hexa`, `ZFLSn1hexa`, `ZFLSn2hexa`, `ZFRSn1hexa`, `ZFRSn2hexa`, `SRSn1hexa`, `SRSn2hexa` ]
.map(x => document.getElementById(x));
numfields =
[ ,,,,,,, `numZFLSn1`, `numZFLSn2`, `numZFRSn1`, `numZFRSn2`, `numSRSn1`, `numSRSn2` ]
.map(x => document.getElementById(x));
// -------------------------------------
// install filters
decfields
.forEach((x, i) => {
// Install input filters (only numbers, value must be a valid Uint32Array element value)
setFilter(x, value => /^[0-9]*$/u.test(value) && value >= 0 && value <= 4294967295);
x.onchange = () => updatefields(i, `d`);
});
binfields
.forEach((x, i) => {
// Install input filters (only 0's or 1's, length of 32 is enforced by ValidityState)
setFilter(x, value => /^[0-1]*$/u.test(value));
x.onchange = () => updatefields(i, `b`);
});
hexfields
.forEach((x, i) => {
// Install input filters (only 0-9 A-F, length of 8 is enforced by ValidityState)
setFilter(x, value => /^[0-9A-F]*$/u.test(value));
x.onchange = () => updatefields(i, `h`);
});
numfields.forEach((x, i) => {
if (typeof x !== `undefined`) {
// Install input filters (only numbers)
setFilter(x, value => /^[0-9]*/u.test(value) && value >= 0 && value <= 32);
x.onchange = () => updatefields(i, `s`);
}
});
// -------------------------------------
};
</script>
</head>
<body onload="initfields()">
<table>
<tr>
<td colspan="3"><h2>Binary and hexadecimal representation of unsigned 32 bit integers</h2></td>
</tr>
<tr>
<td>Number 1 (base 10) :</td>
<td> </td>
<td><input type="number" min="0" max="4294967295" id="n1decimal" class="decimal editable"></td>
<td> </td>
</tr>
<tr>
<td>Number 2 (base 10) :</td>
<td> </td>
<td><input type="number" min="0" max="4294967295" id="n2decimal" class="decimal editable"></td>
<td> </td>
</tr>
<tr>
<td colspan="3">-----------------------------------------------------------------------------------------------------------------</td>
</tr>
<tr>
<td>Number 1 (base 2 and 16) :</td>
<td> </td>
<td>
<input type="text" id="n1binary" minlength="32" maxlength="32" class="binary editable">
<input type="text" id="n1hexa" minlength="8" maxlength="8" class="hexa editable">
</td>
</tr>
<tr>
<td>Number 2 (base 2 and 16) :</td>
<td> </td>
<td>
<input type="text" id="n2binary" minlength="32" maxlength="32" class="binary editable">
<input type="text" id="n2hexa" minlength="8" maxlength="8" class="hexa editable">
</td>
</tr>
<tr>
<td colspan="3">-----------------------------------------------------------------------------------------------------------------</td>
</tr>
<tr>
<td>Number 1 AND Number 2 :</td>
<td> </td>
<td>
<input type="text" id="n1ANDn2binary" minlength="32" maxlength="32" class="binary" readonly="true">
<input type="text" id="n1ANDn2hexa" minlength="8" maxlength="8" class="hexa" readonly="true">
<input type="number" min="0" max="4294967295" id="n1ANDn2decimal" class="decimal" readonly="true">
</td>
</tr>
<tr>
<td>Number 1 OR Number 2 :</td>
<td> </td>
<td>
<input type="text" id="n1ORn2binary" minlength="32" maxlength="32" class="binary" readonly="true">
<input type="text" id="n1ORn2hexa" minlength="8" maxlength="8" class="hexa" readonly="true">
<input type="number" min="0" max="4294967295" id="n1ORn2decimal" class="decimal" readonly="true">
</td>
</tr>
<tr>
<td>Number 1 XOR Number 2 :</td>
<td> </td>
<td>
<input type="text" id="n1XORn2binary" minlength="32" maxlength="32" class="binary" readonly="true">
<input type="text" id="n1XORn2hexa" minlength="8" maxlength="8" class="hexa" readonly="true">
<input type="number" min="0" max="4294967295" id="n1XORn2decimal" class="decimal" readonly="true">
</td>
</tr>
<tr>
<td colspan="3">-----------------------------------------------------------------------------------------------------------------</td>
</tr>
<tr>
<td>NOT Number 1 :</td>
<td> </td>
<td>
<input type="text" id="NOTn1binary" minlength="32" maxlength="32" class="binary" readonly="true">
<input type="text" id="NOTn1hexa" minlength="8" maxlength="8" class="hexa" readonly="true">
<input type="number" min="0" max="4294967295" id="NOTn1decimal" class="decimal" readonly="true">
</td>
</tr>
<tr>
<td>NOT Number 2 :</td>
<td> </td>
<td>
<input type="text" id="NOTn2binary" minlength="32" maxlength="32" class="binary" readonly="true">
<input type="text" id="NOTn2hexa" minlength="8" maxlength="8" class="hexa" readonly="true">
<input type="number" min="0" max="4294967295" id="NOTn2decimal" class="decimal" readonly="true">
</td>
</tr>
<tr>
<td colspan="3">-----------------------------------------------------------------------------------------------------------------</td>
</tr>
<tr>
<td>Number 1 zero fill left shift :</td>
<td><input type="number" min="0" max="32" id="numZFLSn1" class="num" value = "0"></td>
<td>
<input type="text" id="ZFLSn1binary" minlength="32" maxlength="32" class="binary" readonly="true">
<input type="text" id="ZFLSn1hexa" minlength="8" maxlength="8" class="hexa" readonly="true">
<input type="number" min="0" max="4294967295" id="ZFLSn1decimal" class="decimal" readonly="true">
</td>
</tr>
<tr>
<td>Number 2 zero fill left shift :</td>
<td><input type="number" min="0" max="32" id="numZFLSn2" class="num" value = "0"></td>
<td>
<input type="text" id="ZFLSn2binary" minlength="32" maxlength="32" class="binary" readonly="true">
<input type="text" id="ZFLSn2hexa" minlength="8" maxlength="8" class="hexa" readonly="true">
<input type="number" min="0" max="4294967295" id="ZFLSn2decimal" class="decimal" readonly="true">
</td>
</tr>
<tr>
<td colspan="3">-----------------------------------------------------------------------------------------------------------------</td>
</tr>
<tr>
<td>Number 1 zero fill right shift :</td>
<td><input type="number" min="0" max="32" id="numZFRSn1" class="num" value = "0"></td>
<td>
<input type="text" id="ZFRSn1binary" minlength="32" maxlength="32" class="binary" readonly="true">
<input type="text" id="ZFRSn1hexa" minlength="8" maxlength="8" class="hexa" readonly="true">
<input type="number" min="0" max="4294967295" id="ZFRSn1decimal" class="decimal" readonly="true">
</td>
</tr>
<tr>
<td>Number 2 zero fill right shift :</td>
<td><input type="number" min="0" max="32" id="numZFRSn2" class="num" value = "0"></td>
<td>
<input type="text" id="ZFRSn2binary" minlength="32" maxlength="32" class="binary" readonly="true">
<input type="text" id="ZFRSn2hexa" minlength="8" maxlength="8" class="hexa" readonly="true">
<input type="number" min="0" max="4294967295" id="ZFRSn2decimal" class="decimal" readonly="true">
</td>
</tr>
<tr>
<td colspan="3">-----------------------------------------------------------------------------------------------------------------</td>
</tr>
<tr>
<td>Number 1 signed right shift :</td>
<td><input type="number" min="0" max="32" id="numSRSn1" class="num" value = "0"></td>
<td>
<input type="text" id="SRSn1binary" minlength="32" maxlength="32" class="binary" readonly="true">
<input type="text" id="SRSn1hexa" minlength="8" maxlength="8" class="hexa" readonly="true">
<input type="number" min="0" max="4294967295" id="SRSn1decimal" class="decimal" readonly="true">
</td>
</tr>
<tr>
<td>Number 2 signed right shift :</td>
<td><input type="number" min="0" max="32" id="numSRSn2" class="num" value = "0"></td>
<td>
<input type="text" id="SRSn2binary" minlength="32" maxlength="32" class="binary" readonly="true">
<input type="text" id="SRSn2hexa" minlength="8" maxlength="8" class="hexa" readonly="true">
<input type="number" min="0" max="4294967295" id="SRSn2decimal" class="decimal" readonly="true">
</td>
</tr>
<tr>
<td colspan="3"> </td>
</tr>
<tr>
<td colspan="3"><h2>Escaping of UTF-8 / UTF-16 encoded strings (hexadecimal)</h2></td>
</tr>
<tr>
<td>String :</td>
<td colspan="2"><textarea id="s1Flat" class="txtflat editable"></textarea></td>
<td> </td>
</tr>
<tr>
<td colspan="3">-----------------------------------------------------------------------------------------------------------------</td>
</tr>
<tr>
<td>Code points :</td>
<td colspan="2"><textarea id="s1CodePtshexa" class="encoded" readonly="true"></textarea></td>
<td> </td>
</tr>
<tr>
<td colspan="3">-----------------------------------------------------------------------------------------------------------------</td>
</tr>
<tr>
<td>UTF-8 code units (escaped) :</td>
<td colspan="2"><textarea id="s1Utf8hexa" class="encoded" readonly="true"></textarea></td>
<td> </td>
</tr>
<tr>
<td colspan="3">-----------------------------------------------------------------------------------------------------------------</td>
</tr>
<tr>
<td>UTF-16 code units (escaped) :</td>
<td colspan="2"><textarea id="s1Utf16hexa" class="encoded" readonly="true"></textarea></td>
<td> </td>
</tr>
</table>
<br/>
<h4>Recommended for use with Google Chrome 65.0.3325.181</h4>
</body>
</html>