printf没有打印二进制。
自己写了一个,先转成16进制,再转成二进制。
也可以先转成8进制,再转成二进制。
我测试环境double是8个字节,64位长度。
*/
/*打印出double在内存的中的每一个二进制位
*/
void DoubleToString(double *p_dl){
char c_dl_16[16+1];
char c_dl_64[64+1];
memset(c_dl_64,'\0',sizeof(c_dl_64));
unsigned char *t = (unsigned char *)p_dl;
int charCount = sizeof(double) ;
memset(c_dl_16,'\0',sizeof(c_dl_16));
if (t == NULL)
return;
int i=0;
for(i=0;i<charCount;i++){
sprintf(c_dl_16+i*2,"%02x",t[i]);
}
/*printf("\n"); */
printf("[%s]\n",c_dl_16);
/*对每一位,将十六进制转换为二进制的字符串*/
char tmpc[4+1];
for(int j=0;j<16;j++){
memset(tmpc,'\0',sizeof(tmpc));
switch(c_dl_16[j]){
case '0':
memcpy(tmpc,"0000",4);
break;
case '1':
memcpy(tmpc,"0001",4);
break;
case '2':
memcpy(tmpc,"0010",4);
break;
case '3':
memcpy(tmpc,"0011",4);
break;
case '4':
memcpy(tmpc,"0100",4);
break;
case '5':
memcpy(tmpc,"0101",4);
break;
case '6':
memcpy(tmpc,"0110",4);
break;
case '7':
memcpy(tmpc,"0111",4);
break;
case '8':
memcpy(tmpc,"1000",4);
break;
case '9':
memcpy(tmpc,"1001",4);
break;
case 'a':
memcpy(tmpc,"1010",4);
break;
case 'b':
memcpy(tmpc,"1011",4);
break;
case 'c':
memcpy(tmpc,"1100",4);
break;
case 'd':
memcpy(tmpc,"1101",4);
break;
case 'e':
memcpy(tmpc,"1110",4);
break;
case 'f':
memcpy(tmpc,"1111",4);
break;
}
sprintf(c_dl_64+j*4,"%s",tmpc);
}
printf("[%s]\n",c_dl_64);
}
使用
double tmpd=0.01;
DoubleToString(&tmpd);
printf("%x\n",0.01);