70,033
社区成员
 发帖
 发帖 与我相关
 与我相关 我的任务
 我的任务 分享
 分享
#include <stdio.h>
#include <windows.h>
#define set_bit(x,y)    ((x)|=(0x01<<(y)))     //置位x的y位
void check(unsigned char *source, unsigned char *result, unsigned int s_len )
{
        unsigned char No_bit=0, num=0, tem_re=0, tem_s=0;
        while(s_len--)
        {
                tem_s=*source;
                while(tem_s)                     //计算每个字节中1的个数
                  {		
                      tem_s&=tem_s-1;
                      num++;     
                }
                if(num&0x01)                       //检测每个字节的奇偶
                         set_bit(tem_re,No_bit);
                No_bit++;
                if(No_bit>7)
                {
	               No_bit=0;
                       *result++ =tem_re;
                       tem_re=0;
                }
                source++;
                num=0;
       }
}
int main(void)
{
         unsigned char data[]={  0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
	                         0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
                                 0x0e, 0x1e, 0x1e, 0x3e, 0x4e, 0x5e, 0x6e, 0x7e,
                                 0x8e, 0x9e, 0xae, 0xbe, 0xce, 0xde, 0xee, 0xfe,
                               };
         unsigned char *res,d_len=sizeof(data);
         res=(unsigned char *)malloc(d_len>>3);
         check(data, res, d_len);             // 调用校验函数
         while(d_len)                          // 输出校验码数据
         {
	       printf("%d\n",*res++);        
	       d_len-=8;
         }
         return 0;
}
#include <stdio.h>
#include <windows.h>
#define set_bit(x,y)   ((x)|=(0x01<<(y)))     //置位x的y位
void check(unsigned char *source, unsigned char *result, unsigned int s_len ) 
{
        unsigned char No_bit=0, num=0, tem_re=0, tem_s=0;
        while(s_len--)
	{
		tem_s=*source;
		while(tem_s)                     //计算每个字节中1的个数
                  {		
			tem_s&=tem_s-1;
			num++;     
		 }
		if(num&0x01)                       //检测每个字节的奇偶
		        set_bit(tem_re,No_bit);
		No_bit++;
		if(No_bit>7)
		{
		          No_bit=0;
			  *result++ =tem_re;
			  tem_re=0;
		}
		source++;
		num=0;
	}
}
int main(void)
{
         unsigned char data[]={  0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
		                 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
                                 0x0e, 0x1e, 0x1e, 0x3e, 0x4e, 0x5e, 0x6e, 0x7e,
                                 0x8e, 0x9e, 0xae, 0xbe, 0xce, 0xde, 0xee, 0xfe };
         unsigned char *res,d_len=sizeof(data);
         res=(unsigned char *)malloc(d_len>>3);
         check(data, res, d_len);
         while(d_len)
	 {
	      printf("%d\n",*res++);         //输出校验码数据
	       d_len-=8;
	 }
	 return 0;
}
#define set_bit(x,y)   ((x)|=(0x01<<(y)))     //置位x的y位 
#define clr_bit(x,y)   ((x)&=(~(0x01<<(y))))  //清零x的y位 
#define size(a)        (sizeof(a) / sizeof(a[0]))
typedef unsigned char byte;
byte GetEvenVerify(byte bData)
{
    byte i;              /* 循环控制变量 */
    byte bcCount = 0;    /* 字节内1的个数 */
    for(i = 0; i < 8; i++)
    {
        if(bData & 0x01)
        {
            bcCount++;
        }
        bData >>= 1;
    }
    return(bcCount & 0x01);		
}
int main()
{
	byte bData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
	byte bVerify[size(bData) / 8 + 1] = {0}; 
	int  i, len = size(bData); 
	
	for(i = 0; i<len; i++)
	{
		if(GetEvenVerify(bData[i]))
		{
			set_bit(bVerify[i/8], i);
		}
		else
		{	
			clr_bit(bVerify[i/8], i);
		}
	}	
	return 0;
}