Program to convert a Binary Number to Hexa | C ++ code to convert Binary to its HexaDecimal number ( base 16 ) . ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void bToHexaDecimal ( string s ) { int len = s . length ( ) , check = 0 ; int num = 0 , sum = 0 , mul = 1 ; vector < char > ans ; 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 . push_back ( sum + '0' ) ; else ans . push_back ( sum + 55 ) ; check = 0 ; sum = 0 ; mul = 1 ; } } len = ans . size ( ) ; for ( int i = len - 1 ; i >= 0 ; i -- ) cout << ans [ i ] ; } int main ( ) { string s = "100000101111" ; bToHexaDecimal ( s ) ; return 0 ; }