#include <iostream>
//bin(2) -> hex(16)
char* bin_to_hex(const char* b, char* h){
int m, n, i, j;
const char* p = b;
char ch, *q, *t = h;
while(*b && (*b == '0'))
++b;
p = b;
while(*p)
++p;
if(p == b)
*h++ = '0';
n = (int)(p - b);
for(i = n - 1; i > -1; i -= 4){
n = i - 3;
if(n <= -1)
n = 0;
for(m = 0, j = n; j <= i; ++j)
m |= (int)(b[j] - '0') << (i - j);
*h++ = (m < 10) ? (char)(m + '0') : (char)(m - 10 + 'A');
}
*h = '\0';
if(h > t){
for(--h, q = t; h > q; --h, ++q){
ch = *q;
*q = *h;
*h = ch;
}
}
return t;
}
int main(void){
char s[32];
std::cout << bin_to_hex("101", s) << std::endl;
std::cout << bin_to_hex("1000111100001011", s) << std::endl;
std::cout << bin_to_hex("1101101101111001111111101", s) << std::endl;
std::cout << bin_to_hex("1111111100111100111111110100001011111111", s) << std::endl;
return 0;
}