1 | const U8 = new Array(256);
|
2 | const U16 = new Array(256 * 256);
|
3 | for (let n = 0; n < 256; n++) {
|
4 | U8[n] = n.toString(16).padStart(2, '0');
|
5 | }
|
6 | for (let i = 0; i < 256; i++) {
|
7 | const s = i << 8;
|
8 | for (let j = 0; j < 256; j++) {
|
9 | U16[s | j] = U8[i] + U8[j];
|
10 | }
|
11 | }
|
12 |
|
13 | function hex(value, result) {
|
14 | const mod = (value.length % 2) | 0;
|
15 | const length = (value.length - mod) | 0;
|
16 | for (let i = 0; i < length; i += 2) {
|
17 | result += U16[(value[i] << 8) | value[i + 1]];
|
18 | }
|
19 | if (mod) {
|
20 | result += U8[value[length] | 0];
|
21 | }
|
22 | return result;
|
23 | }
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | export function u8aToHex(value, bitLength = -1, isPrefixed = true) {
|
39 |
|
40 | const empty = isPrefixed
|
41 | ? '0x'
|
42 | : '';
|
43 | if (!value?.length) {
|
44 | return empty;
|
45 | }
|
46 | else if (bitLength > 0) {
|
47 | const length = Math.ceil(bitLength / 8);
|
48 | if (value.length > length) {
|
49 | return `${hex(value.subarray(0, length / 2), empty)}…${hex(value.subarray(value.length - length / 2), '')}`;
|
50 | }
|
51 | }
|
52 | return hex(value, empty);
|
53 | }
|