一个小函数,求优化
程序代码:
void hex_n (char *hex) //传入一个十六进制数,将其转换成二进制,我的方法太笨了吧,有没有简单点的。。。 { // char hex_bin_table[][4] = {"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"}; int len_hex = strlen(hex); // int flag = 0; while (len_hex) { switch (*hex) { case '0': printf ("0000 "); break; case '1': printf ("0001 "); break; case '2': printf ("0010 "); break; case '3': printf ("0011 "); break; case '4': printf ("0100 "); break; case '5': printf ("0101 "); break; case '6': printf ("0110 "); break; case '7': printf ("0111 "); break; case '8': printf ("1000 "); break; case '9': printf ("1001 "); break; case 'a': case 'A': printf ("1010 "); break; case 'b': case 'B': printf ("1011 "); break; case 'c': case 'C': printf ("1100 "); break; case 'd': case 'D': printf ("1101 "); break; case 'e': case 'E': printf ("1110 "); break; case 'f': case 'F': printf ("1111 "); break; default: break; } len_hex--; hex++; } }