Program to convert a Binary Number to Hexa | Function to convert Binary to HexaDecimal ; Iterating through the bits backwards ; Computing the HexaDecimal Number formed so far and storing it in a vector . ; Reinitializing all variables for next group . ; Printing the Hexadecimal number formed so far . ; Driver Code ; Function Call
function bToHexaDecimal ( s ) { let len = s . length , check = 0 ; let num = 0 , sum = 0 , mul = 1 ; let ans = new Array ( ) ; for ( let i = len - 1 ; i >= 0 ; i -- ) { sum += ( s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) * mul ; mul *= 2 ; check ++ ; if ( check == 4 i == 0 ) { if ( sum <= 9 ) ans . push ( String . fromCharCode ( sum + ' ' . charCodeAt ( 0 ) ) ) ; else ans . push ( String . fromCharCode ( sum + 55 ) ) ; check = 0 ; sum = 0 ; mul = 1 ; } } len = ans . length ; for ( let i = len - 1 ; i >= 0 ; i -- ) document . write ( ans [ i ] ) ; } let s = " " ; bToHexaDecimal ( s ) ;