Convert String into Binary Sequence | utility function ; convert each char to ASCII value ; Convert ASCII value to binary ; Utility reverse function ; Driver code
function strToBinary ( s ) { let n = s . length ; for ( let i = 0 ; i < n ; i ++ ) { let val = ( s [ i ] ) . charCodeAt ( 0 ) ; let bin = " " ; while ( val > 0 ) { if ( val % 2 == 1 ) { bin += ' ' ; } else bin += ' ' ; val = Math . floor ( val / 2 ) ; } bin = reverse ( bin ) ; document . write ( bin + " " ) ; } } function reverse ( input ) { a = input . split ( " " ) ; let l , r = 0 ; r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { let temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return ( a ) . join ( " " ) ; } let s = " " ; strToBinary ( s ) ;