@@ -33,15 +33,34 @@ export function abool(title: string, value: boolean): void {
33
33
if ( typeof value !== 'boolean' ) throw new Error ( title + ' boolean expected, got ' + value ) ;
34
34
}
35
35
36
+ export function numberToHexUnpadded ( num : number | bigint ) : string {
37
+ const hex = num . toString ( 16 ) ;
38
+ return hex . length & 1 ? '0' + hex : hex ;
39
+ }
40
+
41
+ export function hexToNumber ( hex : string ) : bigint {
42
+ if ( typeof hex !== 'string' ) throw new Error ( 'hex string expected, got ' + typeof hex ) ;
43
+ return hex === '' ? _0n : BigInt ( '0x' + hex ) ; // Big Endian
44
+ }
45
+
46
+ // Built-in hex conversion https://caniuse.com/mdn-javascript_builtins_uint8array_fromhex
47
+ const hasHexBuiltin : boolean =
48
+ // @ts -ignore
49
+ typeof Uint8Array . from ( [ ] ) . toHex === 'function' && typeof Uint8Array . fromHex === 'function' ;
50
+
36
51
// Array where index 0xf0 (240) is mapped to string 'f0'
37
52
const hexes = /* @__PURE__ */ Array . from ( { length : 256 } , ( _ , i ) =>
38
53
i . toString ( 16 ) . padStart ( 2 , '0' )
39
54
) ;
55
+
40
56
/**
57
+ * Convert byte array to hex string. Uses built-in function, when available.
41
58
* @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'
42
59
*/
43
60
export function bytesToHex ( bytes : Uint8Array ) : string {
44
61
abytes ( bytes ) ;
62
+ // @ts -ignore
63
+ if ( hasHexBuiltin ) return bytes . toHex ( ) ;
45
64
// pre-caching improves the speed 6x
46
65
let hex = '' ;
47
66
for ( let i = 0 ; i < bytes . length ; i ++ ) {
@@ -50,16 +69,6 @@ export function bytesToHex(bytes: Uint8Array): string {
50
69
return hex ;
51
70
}
52
71
53
- export function numberToHexUnpadded ( num : number | bigint ) : string {
54
- const hex = num . toString ( 16 ) ;
55
- return hex . length & 1 ? '0' + hex : hex ;
56
- }
57
-
58
- export function hexToNumber ( hex : string ) : bigint {
59
- if ( typeof hex !== 'string' ) throw new Error ( 'hex string expected, got ' + typeof hex ) ;
60
- return hex === '' ? _0n : BigInt ( '0x' + hex ) ; // Big Endian
61
- }
62
-
63
72
// We use optimized technique to convert hex string to byte array
64
73
const asciis = { _0 : 48 , _9 : 57 , A : 65 , F : 70 , a : 97 , f : 102 } as const ;
65
74
function asciiToBase16 ( ch : number ) : number | undefined {
@@ -70,10 +79,13 @@ function asciiToBase16(ch: number): number | undefined {
70
79
}
71
80
72
81
/**
82
+ * Convert hex string to byte array. Uses built-in function, when available.
73
83
* @example hexToBytes('cafe0123') // Uint8Array.from([0xca, 0xfe, 0x01, 0x23])
74
84
*/
75
85
export function hexToBytes ( hex : string ) : Uint8Array {
76
86
if ( typeof hex !== 'string' ) throw new Error ( 'hex string expected, got ' + typeof hex ) ;
87
+ // @ts -ignore
88
+ if ( hasHexBuiltin ) return Uint8Array . fromHex ( hex ) ;
77
89
const hl = hex . length ;
78
90
const al = hl / 2 ;
79
91
if ( hl % 2 ) throw new Error ( 'hex string expected, got unpadded hex of length ' + hl ) ;
0 commit comments