Skip to content

Commit

Permalink
utils: add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Nov 19, 2024
1 parent ec968a9 commit 10254ba
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ export function bytesToHex(bytes: Uint8Array): string {

// We use optimized technique to convert hex string to byte array
const asciis = { _0: 48, _9: 57, _A: 65, _F: 70, _a: 97, _f: 102 } as const;
function asciiToBase16(char: number): number | undefined {
if (char >= asciis._0 && char <= asciis._9) return char - asciis._0;
if (char >= asciis._A && char <= asciis._F) return char - (asciis._A - 10);
if (char >= asciis._a && char <= asciis._f) return char - (asciis._a - 10);
function asciiToBase16(ch: number): number | undefined {
if (ch >= asciis._0 && ch <= asciis._9) return ch - asciis._0; // '2' => 50-48
if (ch >= asciis._A && ch <= asciis._F) return ch - (asciis._A - 10); // 'B' => 66-(65-10)
if (ch >= asciis._a && ch <= asciis._f) return ch - (asciis._a - 10); // 'b' => 98-(97-10)
return;
}

Expand All @@ -62,7 +62,7 @@ export function hexToBytes(hex: string): Uint8Array {
const char = hex[hi] + hex[hi + 1];
throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi);
}
array[ai] = n1 * 16 + n2;
array[ai] = n1 * 16 + n2; // multiply first octet, e.g. 'a3' => 10*16+3 => 160 + 3 => 163
}
return array;
}
Expand Down

0 comments on commit 10254ba

Please sign in to comment.