求把这段C语言的CRC16校验代码转换成C#代码
intCRC16(char*p,intlen){unsignedcharl,h,t;inti;l=h=0xff;for(i=0;i<len;i++){h^=p[i];h^...
int CRC16( char *p, int len )
{
unsigned char l, h, t;
int i;
l=h=0xff;
for( i=0; i<len; i++ )
{
h^=p[i];
h^=h>>4;
t=h;
h=l;
l=t;
t=(l<<4) | (l>>4);
h^=((t<<2) | (t>>6)) & 0x1f;
h^=t&0xf0;
l^=((t<<1) | (t>>7)) & 0xe0;
}
i = ((int)h<<8) | l;
return i;
}
input:2200000C20140730111000000038465E4DA6F447D4992B0ABDD343CC110C02000105000703102015073000000000
output:E337 展开
{
unsigned char l, h, t;
int i;
l=h=0xff;
for( i=0; i<len; i++ )
{
h^=p[i];
h^=h>>4;
t=h;
h=l;
l=t;
t=(l<<4) | (l>>4);
h^=((t<<2) | (t>>6)) & 0x1f;
h^=t&0xf0;
l^=((t<<1) | (t>>7)) & 0xe0;
}
i = ((int)h<<8) | l;
return i;
}
input:2200000C20140730111000000038465E4DA6F447D4992B0ABDD343CC110C02000105000703102015073000000000
output:E337 展开
1个回答
展开全部
static void Main(string[] args)
{
int i = CRC16("2200000C20140730111000000038465E4DA6F447D4992B0ABDD343CC110C02000105000703102015073000000000", "2200000C20140730111000000038465E4DA6F447D4992B0ABDD343CC110C02000105000703102015073000000000".Length);
Console.Write(i.ToString("x"));
Console.Read();
}
static int CRC16(string p, int len)
{
int l, h, t;
int i;
l = h = 0xff;
for (i = 0; i < len; i++)
{
h ^= p[i];
h ^= h >> 4;
t = h;
h = l;
l = t;
t = (l << 4) | (l >> 4);
h ^= ((t << 2) | (t >> 6)) & 0x1f;
h ^= t & 0xf0;
l ^= ((t << 1) | (t >> 7)) & 0xe0;
}
i = ((int)h << 8) | l;
return i;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询