export default class Convert { param { String } str return { Uint8Array } static str2bin ( str ) { str = str . replace ( /\r\n/g , '\n' ) ; const bin = new Uint8Array ( str . length * 3 ) ; let p = 0 ; for ( let i = 0 , len = str . length ; i < len ; i ++ ) { const c = str . charCodeAt ( i ) ; if ( c < 128 ) { bin [ p ++ ] = c ; } else if ( c < 2048 ) { bin [ p ++ ] = ( c >>> 6 ) | 192 ; bin [ p ++ ] = ( c & 63 ) | 128 ; } else { bin [ p ++ ] = ( c >>> 12 ) | 224 ; bin [ p ++ ] = ( ( c >>> 6 ) & 63 ) | 128 ; bin [ p ++ ] = ( c & 63 ) | 128 ; } } return bin . subarray ( 0 , p ) ; } param { Uint8Array } bin return { String } static ab2hex = ( buf ) : string => { return Array . prototype . map . call ( new Uint8Array ( buf ) , x => ( '00' + x . toString ( 16 ) ) . slice ( - 2 ) ) . join ( '' ) ; } ; param { String } bin return { Uint8Array } static hex2ab = ( hex ) : Uint8Array => { const ab = [ ] ; for ( let i = 0 ; i < hex . length ; i += 2 ) { ab . push ( parseInt ( hex . substr ( i , 2 ) , 16 ) ) ; } return new Uint8Array ( ab ) ; } ; param { String } str param { Number } bytes returns static dec2hex = ( str , bytes ) : string => { const decimals = str . toString ( ) . split ( '' ) ; const sum = [ ] ; let hex = [ ] ; let i ; let s ; while ( decimals . length ) { s = 1 * + decimals . shift ( ) ; for ( i = 0 ; s || i < sum . length ; i ++ ) { s += ( sum [ i ] || 0 ) * 10 ; sum [ i ] = s % 16 ; s = ( s - sum [ i ] ) / 16 ; } } while ( sum . length ) { hex . push ( sum . pop ( ) . toString ( 16 ) ) ; } let joined = hex . join ( '' ) ; if ( joined . length % 2 != 0 ) { joined = '0' + joined ; } if ( bytes > joined . length / 2 ) { const diff = bytes - joined . length / 2 ; for ( let i = 0 ; i < diff ; i ++ ) { joined = '00' + joined ; } } return joined ; } ; static dec2bin = ( dec ) : string => { return ( dec >>> 0 ) . toString ( 2 ) ; } ; static bytesToHexString = ( bytes ) : string => { return [ ... bytes ] . map ( b => b . toString ( 16 ) . padStart ( 2 , '0' ) ) . join ( '' ) ; } ; static hexStringToBinary = ( hex ) : string => { return [ ... hex ] . map ( c => ( Convert . dec2bin ( parseInt ( c , 16 ) ) ) . padStart ( 4 , '0' ) ) . join ( '' ) ; } ; static binaryToHexString = ( bin ) : string => { return parseInt ( bin , 2 ) . toString ( 16 ) ; } ; static stringToHex = ( str ) : string => { return [ ... str ] . map ( c => c . charCodeAt ( 0 ) . toString ( 16 ) ) . join ( '' ) ; } ; }