-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
61 lines (44 loc) · 2.02 KB
/
script.js
File metadata and controls
61 lines (44 loc) · 2.02 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
let selectedByteIndex = -1;
function formatHexInput() {
const inputText = document.getElementById("packetInput").value;
const hexArray = inputText.split(' ');
const formattedHexArray = hexArray.map(hex => {
return hex.toUpperCase().replace(/0X/g, '').replace(/,/g, '');
});
const outputText = formattedHexArray.join(' ');
document.getElementById("hexDataView").innerHTML = formatHexView(outputText);
document.getElementById("packetLength").textContent = `Packet Length: ${formattedHexArray.length}`;
const asciiData = formattedHexArray.map(hex => {
if (hex == "00") {
return '\x02';
} else {
return String.fromCharCode(parseInt(hex, 16));
}
});
document.getElementById("asciiDataView").innerHTML = formatAsciiView(asciiData);
}
function formatAsciiView(asciiData) {
return asciiData.map((char, index) => {
return `<span data-index="${index}">${char}</span>`;
}).join(' ');
}
function formatHexView(hexData) {
return hexData.split(' ').map((byte, index) => {
return `<span data-index="${index}">${byte}</span>`;
}).join(' ');
}
let selectedByte = null
document.getElementById("hexDataView").addEventListener("click", function (event) {
if (event.target.tagName === "SPAN") {
if (selectedByte) {
selectedByte.classList.remove("selected");
document.querySelector(`#asciiDataView span[data-index="${selectedByteIndex}"]`).classList.remove("selected");
}
selectedByte = event.target;
selectedByteIndex = parseInt(selectedByte.getAttribute("data-index"), 10);
selectedByte.classList.add("selected");
document.querySelector(`#asciiDataView span[data-index="${selectedByteIndex}"]`).classList.add("selected");
const selectedByteInfo = document.getElementById("selectedByteInfo");
selectedByteInfo.textContent = `Selected Byte Index: ${selectedByteIndex}, ASCII: ${String.fromCharCode(parseInt(selectedByte.textContent, 16))}`;
}
});