-
Notifications
You must be signed in to change notification settings - Fork 0
/
hex.js
37 lines (29 loc) · 847 Bytes
/
hex.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
// This module exports functions for encoding and decoding Uint8Arrays as
// hexadecimal strings.
function encode(bytes) {
// We can not use the Uint8Array's 'map' method, as it attempts to cast our hex
// pairs back to integers.
return Array.from(
bytes,
function hexify(byte) {
return byte.toString(16).padStart(2, "0");
}
).join("").toUpperCase();
}
function decode(string) {
// Each byte is represented as two hexadecimal digits.
let bytes = [];
string.replace(/../g, function (pair) {
bytes.push(parseInt(pair, 16));
});
return new Uint8Array(bytes);
}
if (import.meta.main) {
if (
encode(decode("")) !== ""
|| encode(decode("ff0c10")) !== "FF0C10"
) {
throw new Error("FAIL");
}
}
export default Object.freeze({encode, decode});