通过子函数给字符数组赋值无效,高分!!

jiahehao 2011-06-06 05:40:08
我写了一个子函数,作用是把传进来参数input_char的每一个字节,按位取出来,存到另外一个参数 bit_array中去。

但是在我在主函数中读的时候,发现bit_array中全是空值。求助各位!!!

//convert char into bit
int 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[s] = tmp_bit; 此方法赋完值后打印有值,去主函数打印则无值。
*bit_array++ = tmp_bit; //此方法赋完值后打印即无值。
tmp_char = tmp_char << 1;
s++;
}
i++;
}
*bit_count = s;
printf("bit_array is %s!, bit_count is %d.\n", bit_array, *bit_count);
}

main()
{
char new_comport_data[32];
char bit_data[256];
......
//convert byte to bit;
ConvertToBit(bit_data, new_comport_data, &bit_count);
printf("bit_data is %s!\n", bit_data);
......
}


...全文
202 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ryfdizuo 2011-06-06
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 jiahehao 的回复:]

二楼的dizuo兄弟,你的代码在linux下为什么有的位,读出来是-48,-68,或者49之类的数字呢?而不是象你的,全部打出的是0,1?
[/Quote]
我的那段代码跟系统无关啊,
最终
你想要实现什么效果?
需要注意的是,最终二进制字符串011000010110001001100011是abc对应的acs码的二进制。
a的asc码97,b 98,c 99
jiahehao 2011-06-06
  • 打赏
  • 举报
回复
二楼的dizuo兄弟,你的代码在linux下为什么有的位,读出来是-48,-68,或者49之类的数字呢?而不是象你的,全部打出的是0,1?
就想叫yoko 2011-06-06
  • 打赏
  • 举报
回复
+1[Quote=引用 3 楼 jixingzhong 的回复:]
输出调试时,记得转化输出的字符为可见字符。
或者输出数值,而不是字符。

一般,直接单步调试,查看变量的值会比较直接。
[/Quote]
無_1024 2011-06-06
  • 打赏
  • 举报
回复
单步调试 会得到你想要的结果的 有时候时空格之类的
jixingzhong 2011-06-06
  • 打赏
  • 举报
回复
输出调试时,记得转化输出的字符为可见字符。
或者输出数值,而不是字符。

一般,直接单步调试,查看变量的值会比较直接。
ryfdizuo 2011-06-06
  • 打赏
  • 举报
回复
#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!
请按任意键继续. . .
sduxiaoxiang 2011-06-06
  • 打赏
  • 举报
回复

*bit_array++ = tmp_bit + ‘0’; //此方法赋完值后打印即无值。

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧