Copy codeThe Code is as follows: // ================================================
// Output format: hex2bin 5e.
// Get: 0101 1110
// ================================================
# Include <stdio. h>
# Include <limits. h>
Char * bitstr (char *, void const *, size_t );
Int main (int argc, char ** argv)
{
Unsigned char value;
Char * pointer;
Char ebits [CHAR_BIT * sizeof value + 1];
Value = 0;
For (pointer = argv [1]; * pointer; ++ pointer ){
Value <= 4;
Switch (* pointer ){
Case 'A ':
Case 'A ':
Value + = 0xa;
Break;
Case 'B ':
Case 'B ':
Value + = 0xb;
Break;
Case 'C ':
Case 'C ':
Value + = 0xc;
Break;
Case 'D ':
Case 'D ':
Value + = 0xd;
Break;
Case 'E ':
Case 'E ':
Value + = 0xe;
Break;
Case 'F ':
Case 'F ':
Value + = 0xf;
Break;
Default:
Value + = * pointer-'0 ';
Break;
}
}
Bitstr (ebits, & value, sizeof value );
Printf ("The binary representation of 0x % s is", argv [1]);
For (pointer = ebits, value = 0; * pointer! = '\ 0'; ++ pointer ){
If (value> 3 ){
Value = 0;
Putchar ('');
Putchar (* pointer );
}
Else {
Value ++;
Putchar (* pointer );
}
}
Return 0;
}
Char * bitstr (char * str, const void * obj, size_t n)
{
Unsigned char mask;
Const unsigned char * byte = obj;
Char * const ptr = str;
While (n --){
Mask = (unsigned char)-1> 1) + 1;
Do {
* Str ++ = (char) (mask & byte [n]? '1': '0 ');
Mask> = 1;
} While (mask );
}
* Str = '\ 0 ';
Return ptr;
}