No non-standard library functions, their own hand write a conversion function:
#include <stdio.h> #include <stdlib.h> #include <string.h> static char* CONST_4BITS_LIST[16] = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" }; void convert(char c, char* out){ strcpy(out, CONST_4BITS_LIST[c >> 4 & 0x0f]); strcpy(out+4, CONST_4BITS_LIST[c & 0x0f]); } int main() { char buf[9] = {0}; long long int a = 0x0123456789abcdef; char* p = (char*)&a; for(int i=0; i<sizeof(a); i++){ convert(*(p+i), buf); printf("%s\n", buf); } return 0; }
Again a C ++ is:
#include <cstdio> #include <cstdlib> #include <cstring> static const char* CONST_4BITS_LIST[16] = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" }; void convert(char c, char* out){ strcpy(out, CONST_4BITS_LIST[c >> 4 & 0x0f]); strcpy(out+4, CONST_4BITS_LIST[c & 0x0f]); } int main() { char buf[9] = {0}; long long int a = 0x0123456789abcdef; char* p = (char*)&a; for(int i=0; i<sizeof(a); i++){ convert(*(p+i), buf); printf("%s\n", buf); } return 0; }