帮助翻译几行C语言 [英] Help in translating a few lines of C

查看:91
本文介绍了帮助翻译几行C语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户,我们需要使用他们的

格式发送数据文件。下面是他们发送给我们计算CRC的C例程。我用b $ b将它转换为VB6的最佳能力,但无法重现他们的

结果。


这是C例程。


#define CRC16_POLYNOMIAL 0xA001

crc16(INT16U byte,INT16U crc)

{

INT16U i;

INT16U crc_bit16;

for(i = 0;< 8; i ++)

{

crc_bit16 =((crc& 0x01)^(byte& 0x01));

crc = crc>> 1;

if(crc_bit16)

{

crc =(crc ^ CRC16_POLYNOMIAL);

}

byte = byte>> 1;

}

返回(crc);

}


这是我的VB6代码


函数CRC(CRCIn为长,ByteIn为字节)为长


Const CRC16Polynomial =& HA001

Dim CRCBit16 As整数

Dim I As Integer

Dim WrkByte整数

Dim WrkCRC As Long


WrkByte = ByteIn

WrkCRC = CRCIn

I = 0到7

CRCBit16 =((WrkCRC和& H1)Xor(WrkByte和& H1))

WrkCRC = ShiftRight(WrkCRC,1)

如果CRCBit16<> 0然后

WrkCRC =(WrkCRC Xor CRC16Polynomial)

结束如果

WrkByte = ShiftRight(WrkByte,1)

接下来我

CRC = WrkCRC


END函数


这是通过每个字节的例程CRC例程。


CRCIn = 0

CRC16 = 0

打开C:\vbasic \ shift。 src\Page0.bin"对于Binary作为#1

对于I = 1到30

A $ =输入(1,1)

ByteIn = Asc(A $ )

CRCIn = CRC16

CRC16 = CRC(CRCIn,ByteIn)

接下来我


Page0.bin是一个32字节的小文件,如下所示。最后2个字节是由我的客户C例程计算的CRC。通过查看

更多的代码,看起来CRC使用C'的逆运算符存储为一些

的反转原因。

31 07 49 6E 73 74 72 6F

6E 06 49 6E 53 70 65 63

FF FF FF FF FF FF FF

FF FF FF FF FF FF 19 95

我认为错误的来源是我的ShiftRight例程。我尝试了大约6种不同的例程声称是C'>> equivelent,总是

得到相同的结果。任何帮助表示赞赏。

I have a customer that we need to send data files to using their
format. Below is a C routine they sent us for computing a CRC. I
tranlated it to the best of my ability to VB6 but can''t reproduce their
results.

Here is the C routine.

#define CRC16_POLYNOMIAL 0xA001
crc16(INT16U byte, INT16U crc)
{
INT16U i;
INT16U crc_bit16;
for (i=0;<8;i++)
{
crc_bit16=((crc&0x01)^(byte&0x01));
crc=crc>>1;
if (crc_bit16)
{
crc=(crc^CRC16_POLYNOMIAL);
}
byte=byte>>1;
}
return(crc);
}

Here is my VB6 code

Function CRC(CRCIn As Long, ByteIn As Byte) As Long

Const CRC16Polynomial = &HA001
Dim CRCBit16 As Integer
Dim I As Integer
Dim WrkByte As Integer
Dim WrkCRC As Long

WrkByte = ByteIn
WrkCRC = CRCIn
For I = 0 To 7
CRCBit16 = ((WrkCRC And &H1) Xor (WrkByte And &H1))
WrkCRC = ShiftRight(WrkCRC, 1)
If CRCBit16 <> 0 Then
WrkCRC = (WrkCRC Xor CRC16Polynomial)
End If
WrkByte = ShiftRight(WrkByte, 1)
Next I
CRC = WrkCRC

END Function

And here is the routine that passes each byte through the CRC routine.

CRCIn = 0
CRC16 = 0
Open "C:\vbasic\shift.src\Page0.bin" For Binary As #1
For I = 1 To 30
A$ = Input(1, 1)
ByteIn = Asc(A$)
CRCIn = CRC16
CRC16 = CRC(CRCIn, ByteIn)
Next I

Page0.bin is a small 32 byte file as shown below. The last 2 bytes are
the CRC as computed by my customers C routine. From looking through
more of their code it appears the CRC is stored as an inverse for some
reason using C''s ~ inverse operator.

31 07 49 6E 73 74 72 6F
6E 06 49 6E 53 70 65 63
FF FF FF FF FF FF FF FF
FF FF FF FF FF FF 19 95

I thought the source of the error is in my ShiftRight routine. I tried
roughly 6 different routines claiming to be C''s >> equivelent and always
get the same results. Any help is appreciated.

推荐答案

=输入(1,1)

ByteIn = Asc(A
= Input(1, 1)
ByteIn = Asc(A




CRCIn = CRC16

CRC16 = CRC(CRCIn,ByteIn)

下一页


Page0.bin是一个32字节的小文件,如下所示。最后2个字节是由我的客户C例程计算的CRC。通过查看

更多的代码,看起来CRC使用C'的逆运算符存储为一些

的反转原因。

31 07 49 6E 73 74 72 6F

6E 06 49 6E 53 70 65 63

FF FF FF FF FF FF FF

FF FF FF FF FF FF 19 95

我认为错误的来源是我的ShiftRight例程。我尝试了大约6种不同的例程声称是C'>> equivelent,总是

得到相同的结果。感谢任何帮助。

)
CRCIn = CRC16
CRC16 = CRC(CRCIn, ByteIn)
Next I

Page0.bin is a small 32 byte file as shown below. The last 2 bytes are
the CRC as computed by my customers C routine. From looking through
more of their code it appears the CRC is stored as an inverse for some
reason using C''s ~ inverse operator.

31 07 49 6E 73 74 72 6F
6E 06 49 6E 53 70 65 63
FF FF FF FF FF FF FF FF
FF FF FF FF FF FF 19 95

I thought the source of the error is in my ShiftRight routine. I tried
roughly 6 different routines claiming to be C''s >> equivelent and always
get the same results. Any help is appreciated.


milesh写道:
我有一个客户,我们需要使用他们的<发送数据文件格式。下面是他们发送给我们计算CRC的C例程。我尽可能地将它转换为VB6的能力,但无法重现他们的结果。

这是C例程。
I have a customer that we need to send data files to using their
format. Below is a C routine they sent us for computing a CRC. I
tranlated it to the best of my ability to VB6 but can''t reproduce their
results.

Here is the C routine.



< snip>


有些人可能最终会回答这个问题,但我认为这也是适合在编译程序中发布的b $ b同样。


<snip>

Some people might eventually answer this question, but I think it would
also be appropriate to post in comp.programming as well.


这篇关于帮助翻译几行C语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆