import reduce from 'lodash/reduce' ; import isEmpty from 'lodash/isEmpty' ; param { number } byte returns { string } const byteToHex = ( byte ) : string => ( byte < 16 ? '0' : '' ) + byte . toString ( 16 ) ; param { number [ ] } byteArray returns { string } const byteArrayToHex = ( byteArray ) : string => byteArray . map ( ( byte ) => byteToHex ( byte ) ) . join ( '' ) ; param { string } hex returns { number [ ] } const hexToByteArray = ( hex ) : number [ ] => { if ( isEmpty ( hex ) ) return [ ] ; const chunk = hex . match ( /.{2}/g ) ; return reduce ( chunk , ( memo , curr ) => { memo . push ( parseInt ( curr , 16 ) ) ; return memo ; } , [ ] as number [ ] ) ; } ; export { byteArrayToHex , hexToByteArray } ;