FD小工具代码-hex2bin

为了某件事写了个小工具,顺手扔进FD工具箱中。


/*
输入十六进制,输出二进制
*/#include <ctype.h>
#include <stdio.h>#define H0 "0000 "
#define H1 "0001 "
#define H2 "0010 "
#define H3 "0011 "
#define H4 "0100 "
#define H5 "0101 "
#define H6 "0110 "
#define H7 "0111 "
#define H8 "1000 "
#define H9 "1001 "
#define Ha "1010 "
#define Hb "1011 "
#define Hc "1100 "
#define Hd "1101 "
#define He "1110 "
#define Hf "1111 "void hex2bin(char string[]){for(;*string != 0;string++){char temp = toupper(*string);switch (temp){case '0':printf(H0);break;case '1':printf(H1);break;case '2':printf(H2);break;case '3':printf(H3);break;case '4':printf(H4);break;case '5':printf(H5);break;case '6':printf(H6);break;case '7':printf(H7);break;case '8':printf(H8);break;case '9':printf(H9);break;case 'A':printf(Ha);break;case 'B':printf(Hb);break;case 'C':printf(Hc);break;case 'D':printf(Hd);break;case 'E':printf(He);break;case 'F':printf(Hf);break;default:printf("ERR");return;break;}}	
}int main(int argc, char **argv){for(int i = 1; i <argc; i++){printf("0x%s: ", argv[i]);hex2bin(argv[i]);puts("");}return 0;
}


输出示例:


bogon:desktop firedom$ ./FDhex2bin 6000 1234 fdfdfdfd abcdefg

0x6000: 0110 0000 0000 0000 

0x1234: 0001 0010 0011 0100 

0xfdfdfdfd: 1111 1101 1111 1101 1111 1101 1111 1101 

0xabcdefg: 1010 1011 1100 1101 1110 1111 ERR


本文链接:https://my.lmcjl.com/post/6456.html

展开阅读全文

4 评论

留下您的评论.