Merge pull request #1021 from cosmos/improve-fromHex

Improve fromHex implementation
This commit is contained in:
Simon Warta 2022-01-30 10:59:15 +01:00 committed by GitHub
commit d9df9dae8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,13 +11,14 @@ export function fromHex(hexstring: string): Uint8Array {
throw new Error("hex string length must be a multiple of 2");
}
const listOfInts: number[] = [];
for (let i = 0; i < hexstring.length; i += 2) {
const hexByteAsString = hexstring.substr(i, 2);
const out = new Uint8Array(hexstring.length / 2);
for (let i = 0; i < out.length; i++) {
const j = 2 * i;
const hexByteAsString = hexstring.slice(j, j + 2);
if (!hexByteAsString.match(/[0-9a-f]{2}/i)) {
throw new Error("hex string contains invalid characters");
}
listOfInts.push(parseInt(hexByteAsString, 16));
out[i] = parseInt(hexByteAsString, 16);
}
return new Uint8Array(listOfInts);
return out;
}