| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 439 人关注过本帖
标题:各位大侠帮忙转为VB6能用的代码
只看楼主 加入收藏
kingslink
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-8-11
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
各位大侠帮忙转为VB6能用的代码
#define CRC(crc,byte) (((crc) >> 8 ) ^ tabel[((crc) ^ (unsigned int) (byte)) & 0XFF])
static const uint16 tabel[256] = {
0X0000, 0XC0C1, 0XC181, 0X0140, 0XC301, 0X03C0, 0X0280, 0XC241,
0XC601, 0X06C0, 0X0780, 0XC741, 0X0500, 0XC5C1, 0XC481, 0X0440,
0XCC01, 0X0CC0, 0X0D80, 0XCD41, 0X0F00, 0XCFC1, 0XCE81, 0X0E40,
0X0A00, 0XCAC1, 0XCB81, 0X0B40, 0XC901, 0X09C0, 0X0880, 0XC841,
0XD801, 0X18C0, 0X1980, 0XD941, 0X1B00, 0XDBC1, 0XDA81, 0X1A40,
0X1E00, 0XDEC1, 0XDF81, 0X1F40, 0XDD01, 0X1DC0, 0X1C80, 0XDC41,
0X1400, 0XD4C1, 0XD581, 0X1540, 0XD701, 0X17C0, 0X1680, 0XD641,
0XD201, 0X12C0, 0X1380, 0XD341, 0X1100, 0XD1C1, 0XD081, 0X1040,
0XF001, 0X30C0, 0X3180, 0XF141, 0X3300, 0XF3C1, 0XF281, 0X3240,
0X3600, 0XF6C1, 0XF781, 0X3740, 0XF501, 0X35C0, 0X3480, 0XF441,
0X3C00, 0XFCC1, 0XFD81, 0X3D40, 0XFF01, 0X3FC0, 0X3E80, 0XFE41,
0XFA01, 0X3AC0, 0X3B80, 0XFB41, 0X3900, 0XF9C1, 0XF881, 0X3840,
0X2800, 0XE8C1, 0XE981, 0X2940, 0XEB01, 0X2BC0, 0X2A80, 0XEA41,
0XEE01, 0X2EC0, 0X2F80, 0XEF41, 0X2D00, 0XEDC1, 0XEC81, 0X2C40,
0XE401, 0X24C0, 0X2580, 0XE541, 0X2700, 0XE7C1, 0XE681, 0X2640,
0X2200, 0XE2C1, 0XE381, 0X2340, 0XE101, 0X21C0, 0X2080, 0XE041,
0XA001, 0X60C0, 0X6180, 0XA141, 0X6300, 0XA3C1, 0XA281, 0X6240,
0X6600, 0XA6C1, 0XA781, 0X6740, 0XA501, 0X65C0, 0X6480, 0XA441,
0X6C00, 0XACC1, 0XAD81, 0X6D40, 0XAF01, 0X6FC0, 0X6E80, 0XAE41,
0XAA01, 0X6AC0, 0X6B80, 0XAB41, 0X6900, 0XA9C1, 0XA881, 0X6840,
0X7800, 0XB8C1, 0XB981, 0X7940, 0XBB01, 0X7BC0, 0X7A80, 0XBA41,
0XBE01, 0X7EC0, 0X7F80, 0XBF41, 0X7D00, 0XBDC1, 0XBC81, 0X7C40,
0XB401, 0X74C0, 0X7580, 0XB541, 0X7700, 0XB7C1, 0XB681, 0X7640,
0X7200, 0XB2C1, 0XB381, 0X7340, 0XB101, 0X71C0, 0X7080, 0XB041,
0X5000, 0X90C1, 0X9181, 0X5140, 0X9301, 0X53C0, 0X5280, 0X9241,
0X9601, 0X56C0, 0X5780, 0X9741, 0X5500, 0X95C1, 0X9481, 0X5440,
0X9C01, 0X5CC0, 0X5D80, 0X9D41, 0X5F00, 0X9FC1, 0X9E81, 0X5E40,
0X5A00, 0X9AC1, 0X9B81, 0X5B40, 0X9901, 0X59C0, 0X5880, 0X9841,
0X8801, 0X48C0, 0X4980, 0X8941, 0X4B00, 0X8BC1, 0X8A81, 0X4A40,
0X4E00, 0X8EC1, 0X8F81, 0X4F40, 0X8D01, 0X4DC0, 0X4C80, 0X8C41,
0X4400, 0X84C1, 0X8581, 0X4540, 0X8701, 0X47C0, 0X4680, 0X8641,
0X8201, 0X42C0, 0X4380, 0X8341, 0X4100, 0X81C1, 0X8081, 0X4040
};


unsigned short CalcCRC(uint8 *data, uint32 size)//
{
uint32 i;
unsigned short crc = 0;
for (i = 0; i < size; i++) {
crc = CRC(crc, data[i]);
}
return crc;
}


在text1中输入“01 00 00 80 00 00 00 00 00 00 FE 02 26 00 A3 06 01 21 00 00 01 1D 00 00 00 00 00 00 0A 00 40 00 00 00 00 02 00 00 00 00 02 02 01 00 00 0A 02 00 00 00 A1 E6”

在text2中要显示校验码:“ACF2”

用上面的C语言代码转VB6要怎么写,谢谢各位,感激涕零!
2015-08-11 12:59
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:20 
用C语言代码编译为DLL,然后到 VB6 里引用 。

#define CRC(crc,byte) (((crc) >> 8 ) ^ tabel[((crc) ^ (unsigned int) (byte)) & 0XFF])
这句没看懂。

我写的代码有问题。关键就是这句没看懂。
没时间调试了,你自己慢慢动吧。

运算符,VB没有 右移和左移,
数据类型,VB没有 无符号数
          VB有BYTE,但没有能把一个 integer 变成二个BYTE的函数,要自己写。

下面的代码你参考一下吧。
出错的函数就是:
Public Function CRC(c1 As Integer, c2 As Integer) As Integer



--------------------
程序代码:
Option Explicit
Dim tabel(255) As Integer

Private Sub Command1_Click()
Dim a() As Integer
Dim fj() As String
Dim i As Long
Dim j As Long
Dim s As String

s = Text1.Text
fj = Split(s, " ")
j = UBound(fj)
ReDim a(j)
For i = 0 To j
    a(i) = Val("&H" & fj(i) & "&")
Next i
Text2.Text = Hex(CalcCRC(a(), j))
End Sub

Private Sub Form_Load()
Dim a() As Variant
a = Array( _
&H0&, &HC0C1&, &HC181&, &H140&, &HC301&, &H3C0&, &H280&, &HC241&, &HC601&, &H6C0&, &H780&, _
&HC741&, &H500&, &HC5C1&, &HC481&, &H440&, &HCC01&, &HCC0&, &HD80&, &HCD41&, &HF00&, &HCFC1&, _
&HCE81&, &HE40&, &HA00&, &HCAC1&, &HCB81&, &HB40&, &HC901&, &H9C0&, &H880&, &HC841&, &HD801&, _
&H18C0&, &H1980&, &HD941&, &H1B00&, &HDBC1&, &HDA81&, &H1A40&, &H1E00&, &HDEC1&, &HDF81&, &H1F40&, _
&HDD01&, &H1DC0&, &H1C80&, &HDC41&, &H1400&, &HD4C1&, &HD581&, &H1540&, &HD701&, &H17C0&, &H1680&, _
&HD641&, &HD201&, &H12C0&, &H1380&, &HD341&, &H1100&, &HD1C1&, &HD081&, &H1040&, &HF001&, &H30C0&, _
&H3180&, &HF141&, &H3300&, &HF3C1&, &HF281&, &H3240&, &H3600&, &HF6C1&, &HF781&, &H3740&, &HF501&, _
&H35C0&, &H3480&, &HF441&, &H3C00&, &HFCC1&, &HFD81&, &H3D40&, &HFF01&, &H3FC0&, &H3E80&, &HFE41&, _
&HFA01&, &H3AC0&, &H3B80&, &HFB41&, &H3900&, &HF9C1&, &HF881&, &H3840&, &H2800&, &HE8C1&, &HE981&, _
&H2940&, &HEB01&, &H2BC0&, &H2A80&, &HEA41&, &HEE01&, &H2EC0&, &H2F80&, &HEF41&, &H2D00&, &HEDC1&, _
&HEC81&, &H2C40&, &HE401&, &H24C0&, &H2580&, &HE541&, &H2700&, &HE7C1&, &HE681&, &H2640&, &H2200&, _
&HE2C1&, &HE381&, &H2340&, &HE101&, &H21C0&, &H2080&, &HE041&, &HA001&, &H60C0&, &H6180&, &HA141&, _
&H6300&, &HA3C1&, &HA281&, &H6240&, &H6600&, &HA6C1&, &HA781&, &H6740&, &HA501&, &H65C0&, &H6480&, _
&HA441&, &H6C00&, &HACC1&, &HAD81&, &H6D40&, &HAF01&, &H6FC0&, &H6E80&, &HAE41&, &HAA01&, &H6AC0&, _
&H6B80&, &HAB41&, &H6900&, &HA9C1&, &HA881&, &H6840&, &H7800&, &HB8C1&, &HB981&, &H7940&, &HBB01&, _
&H7BC0&, &H7A80&, &HBA41&, &HBE01&, &H7EC0&, &H7F80&, &HBF41&, &H7D00&, &HBDC1&, &HBC81&, &H7C40&, _
&HB401&, &H74C0&, &H7580&, &HB541&, &H7700&, &HB7C1&, &HB681&, &H7640&, &H7200&, &HB2C1&, &HB381&, _
&H7340&, &HB101&, &H71C0&, &H7080&, &HB041&, &H5000&, &H90C1&, &H9181&, &H5140&, &H9301&, &H53C0&, _
&H5280&, &H9241&, &H9601&, &H56C0&, &H5780&, &H9741&, &H5500&, &H95C1&, &H9481&, &H5440&, &H9C01&, _
&H5CC0&, &H5D80&, &H9D41&, &H5F00&, &H9FC1&, &H9E81&, &H5E40&, &H5A00&, &H9AC1&, &H9B81&, &H5B40&, _
&H9901&, &H59C0&, &H5880&, &H9841&, &H8801&, &H48C0&, &H4980&, &H8941&, &H4B00&, &H8BC1&, &H8A81&, _
&H4A40&, &H4E00&, &H8EC1&, &H8F81&, &H4F40&, &H8D01&, &H4DC0&, &H4C80&, &H8C41&, &H4400&, &H84C1&, _
&H8581&, &H4540&, &H8701&, &H47C0&, &H4680&, &H8641&, &H8201&, &H42C0&, &H4380&, &H8341&, &H4100&, _
&H81C1&, &H8081&, &H4040&)

Dim i As Long
For i = 0 To 255
    If a(i) <= 32767 Then
        tabel(i) = a(i)
    Else
        tabel(i) = a(i) - 32767 - 1 - 32767 - 1         '第一次干掉最高位,第二位取反,得负数,转16进制数没错
        'VB的 integer 不支持无符号数
    End If
Next i
End Sub

Public Function CRC(c1 As Integer, c2 As Integer) As Integer

'这句没看懂
'#define CRC(crc,byte) (  ((crc) >> 8 ) ^ tabel[((crc) ^ (unsigned int) (byte)) & 0XFF])

CRC = ((c1 / 255) And &HFF&) Xor (tabel(((c1 And &HFF&) And (c2 And &HFF&)) Or &HFF&))

End Function

Public Function CalcCRC(cs() As Integer, size As Long) As Integer
Dim i As Long
Dim c As Integer

For i = 0 To size
    c = CRC(c, cs(i))
Next i
CalcCRC = c

End Function

授人于鱼,不如授人于渔
早已停用QQ了
2015-08-11 17:39
快速回复:各位大侠帮忙转为VB6能用的代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035862 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved