Program to convert a Binary Number to Hexa | Java code to convert BCD to its HexaDecimal number ( base 16 ) . Including Header Files ; Function to convert BCD 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
import java . util . * ; class GFG { static void bcdToHexaDecimal ( char [ ] s ) { int len = s . length , check = 0 ; int num = 0 , sum = 0 , mul = 1 ; Vector < Character > ans = new Vector < Character > ( ) ; for ( int i = len - 1 ; i >= 0 ; i -- ) { sum += ( s [ i ] - '0' ) * mul ; mul *= 2 ; check ++ ; if ( check == 4 i == 0 ) { if ( sum <= 9 ) ans . add ( ( char ) ( sum + '0' ) ) ; else ans . add ( ( char ) ( sum + 55 ) ) ; check = 0 ; sum = 0 ; mul = 1 ; } } len = ans . size ( ) ; for ( int i = len - 1 ; i >= 0 ; i -- ) System . out . print ( ans . get ( i ) ) ; } public static void main ( String [ ] args ) { String s = "100000101111" ; bcdToHexaDecimal ( s . toCharArray ( ) ) ; } }