Copy Code code 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!= '; ++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 = ' n ';
return ptr;
}