Skip to content

Commit

Permalink
utils.bytesToHex: small change
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Nov 21, 2024
1 parent d356a1d commit 421ebfa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ 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;
const asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 } as const;
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)
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 Down
10 changes: 9 additions & 1 deletion test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ const fc = require('fast-check');
const { describe, should } = require('micro-should');
const { optional, integer, gen } = require('./generator');
const { TYPE_TEST } = require('./utils');
const { byteSwap, byteSwapIfBE, byteSwap32, isLE, bytesToHex, concatBytes, hexToBytes } = require('../utils');
const {
byteSwap,
byteSwapIfBE,
byteSwap32,
isLE,
bytesToHex,
concatBytes,
hexToBytes,
} = require('../utils');

describe('utils', () => {
const staticHexVectors = [
Expand Down

0 comments on commit 421ebfa

Please sign in to comment.