(hex: string)
| 86 | * ``` |
| 87 | */ |
| 88 | export function hexToBytes(hex: string): Uint8Array { |
| 89 | // Remove whitespace |
| 90 | const clean = hex.replace(/\s/g, ""); |
| 91 | |
| 92 | // Pad odd-length with trailing 0 |
| 93 | const padded = clean.length % 2 === 1 ? `${clean}0` : clean; |
| 94 | |
| 95 | const bytes = new Uint8Array(padded.length / 2); |
| 96 | |
| 97 | for (let i = 0; i < bytes.length; i++) { |
| 98 | bytes[i] = Number.parseInt(padded.slice(i * 2, i * 2 + 2), 16); |
| 99 | } |
| 100 | |
| 101 | return bytes; |
| 102 | } |