-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversions.js
65 lines (59 loc) · 1.59 KB
/
conversions.js
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
tle_btn = document.getElementById('toLittleEndianBtn');
tle_in = document.getElementById('toLittleEndian');
tle_op = document.getElementById('toLittleEndianOutput');
abs_btn = document.getElementById('addBackslashBtn');
abs_in = document.getElementById('addBackslash');
abs_op = document.getElementById('addBackslashOutput');
tle_btn.addEventListener('click', () => {
input = document.getElementById('toLittleEndian').value;
if (input.substring(0,2) !== "0x"){
console.log("!")
} else {
input = input.substring(2);
}
tle_op.value = littleEndian(input);
});
abs_btn.addEventListener('click', () => {
input = document.getElementById('addBackslash').value;
abs_op.value = add_backslash(input);
});
// execute same code on enter as well
tle_in.addEventListener("keyup", (event) => {
if (event.keyCode === 13) {
event.preventDefault();
tle_btn.click();
}
});
abs_in.addEventListener("keyup", (event) => {
if (event.keyCode === 13) {
event.preventDefault();
abs_btn.click();
}
});
let littleEndian = (address) => {
if (address.length < 8){
zeros = "0".repeat(8 - address.length);
address = zeros + address;
}
return(add_backslash(reverse(address)));
}
let reverse = (address) => {
add = "".repeat(8);
i = address.length - 2; // 6
j = 0;
while (i >= 0){
add = add.substring(0, j) + address.substring(i,i+2) + add.substring(j+2);
j+=2;
i-=2;
}
return add;
}
let add_backslash = (address) => {
add = "";
for (i = 0; i < address.length; i+=2){
if (i % 2 == 0)
add += "\\x";
add += address.substring(i,i+2);
}
return add;
}