69,373
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
//convert char into bit
void ConvertToBit(char * bit_array, char * input_char, int * bit_count)
{
int i = 0;
int j = 0;
int s = 0;
unsigned char tmp_char, tmp_bit;
while( input_char[i] != '\0')
{
//printf("the input_char is %d.\n", input_char[0]);
tmp_char = input_char[i];
for(j = 0; j < 8; j++)
{
tmp_bit = (tmp_char & (1 << 7));
tmp_bit = (tmp_char >> 7);
printf("the tmp_bit is %d.\n",tmp_bit);
*bit_array++ = tmp_bit + '0'; // 这里,将asc码数值转化为char, asc码0,1是不可打印的。。。
tmp_char = tmp_char << 1;
s++;
}
i++;
}
*bit_count = s;
printf("bit_array is %s!, bit_count is %d.\n", bit_array, *bit_count);
}
void main()
{
char new_comport_data[32] = {"abc"};
char bit_data[256] = {0}; //初始化
int bit_count = 0;
//convert byte to bit;
ConvertToBit(bit_data, new_comport_data, &bit_count);
printf("bit_data is %s!\n", bit_data);
system("PAUSE");
}
the tmp_bit is 0.
the tmp_bit is 1.
the tmp_bit is 1.
the tmp_bit is 0.
the tmp_bit is 0.
the tmp_bit is 0.
the tmp_bit is 0.
the tmp_bit is 1.
the tmp_bit is 0.
the tmp_bit is 1.
the tmp_bit is 1.
the tmp_bit is 0.
the tmp_bit is 0.
the tmp_bit is 0.
the tmp_bit is 1.
the tmp_bit is 0.
the tmp_bit is 0.
the tmp_bit is 1.
the tmp_bit is 1.
the tmp_bit is 0.
the tmp_bit is 0.
the tmp_bit is 0.
the tmp_bit is 1.
the tmp_bit is 1.
bit_array is !, bit_count is 24.
bit_data is 011000010110001001100011!
请按任意键继续. . .
*bit_array++ = tmp_bit + ‘0’; //此方法赋完值后打印即无值。