1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.hexToU8a = void 0;
|
4 | const CHR = '0123456789abcdef';
|
5 | const U8 = new Uint8Array(256);
|
6 | const U16 = new Uint8Array(256 * 256);
|
7 | for (let i = 0, count = CHR.length; i < count; i++) {
|
8 | U8[CHR[i].charCodeAt(0) | 0] = i | 0;
|
9 | if (i > 9) {
|
10 | U8[CHR[i].toUpperCase().charCodeAt(0) | 0] = i | 0;
|
11 | }
|
12 | }
|
13 | for (let i = 0; i < 256; i++) {
|
14 | const s = i << 8;
|
15 | for (let j = 0; j < 256; j++) {
|
16 | U16[s | j] = (U8[i] << 4) | U8[j];
|
17 | }
|
18 | }
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | function hexToU8a(value, bitLength = -1) {
|
35 | if (!value) {
|
36 | return new Uint8Array();
|
37 | }
|
38 | let s = value.startsWith('0x')
|
39 | ? 2
|
40 | : 0;
|
41 | const decLength = Math.ceil((value.length - s) / 2);
|
42 | const endLength = Math.ceil(bitLength === -1
|
43 | ? decLength
|
44 | : bitLength / 8);
|
45 | const result = new Uint8Array(endLength);
|
46 | const offset = endLength > decLength
|
47 | ? endLength - decLength
|
48 | : 0;
|
49 | for (let i = offset; i < endLength; i++, s += 2) {
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | result[i] = U16[(value.charCodeAt(s) << 8) | value.charCodeAt(s + 1)];
|
55 | }
|
56 | return result;
|
57 | }
|
58 | exports.hexToU8a = hexToU8a;
|