Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checksums in VB

Status
Not open for further replies.

AdaHacker

Programmer
Sep 6, 2001
392
US
I'm working on an app that retreives data through a modem. Since my tests show I'm getting some errors, I'd like to use the remote system's interactive capabilities, which sends a CRC-16 checksum after the message and waits for confirmation before proceeding.
My problem is, I'm at a loss as to how to implement the CRC algorithm in VB. The algorithm involves lots of bit shifting, which is easy enough in C, but I have no idea how to do it in VB.
I considered converting the data to a string or 1's and 0's which I could manipulate, but besides being a big pain, I'm not sure it would be feasible in terms of speed.
So what should I do? Has anyone ever seen a CRC-16 implementation in VB? How would one handle the bit shifting? Is there some kind of shift function in VB? Can someone suggest a way to simulate it?
 
SHIFT LEFT = *2
SHIFT RITE = /2

Assuming 1 word Integers:[tt]
function shl(v)as integer
shl = (v and &H7FFFFFFF) *2
end function
function shr(v) as integer
shr = (v and &HFFFFFFFE) /2
end function
[/tt]
The logiocal operations AND, NOT, OR etc are bitwise when working with integers.

VB might have primatives DUNNO.

Wil Mead
wmead@optonline.net

 
Code:
' Do entire message, parts of message or byte by byte.
Save CRC betweeen calls. Make CRC = 0 to start fresh.
pCRC = Compute_CRC(pCRC, pMsg)
'.......
Function Compute_CRC&(pCRC&, pMsg$)
    Dim wCRC&, wDH&, wDL&, wBL&, wBH&, I%, J%
    Dim wCRCT&
    Static sCRCInit%, sCRCT&(0 To 255)
    If sCRCInit = False Then
        INIT_CRC sCRCT()
        sCRCInit = True
    End If
   
    J = Len(pMsg)         ' Get Length of buffer
    wCRC = pCRC           ' CRC from previous in LO-HI
    For I = 1 To J
        wDL = wCRC \ 256
        wDH = wCRC And &HFF
        wDL = (Asc(Mid$(pMsg, I, 1)) Xor wDL) And &HFF
        wBL = wDL
        wDL = wDH
        wDH = 0
        wCRCT = sCRCT(wBL)            ' Get CRC Table Byte
        wBL = wCRCT And &HFF
        wBH = wCRCT \ 256
        wDH = (wBH Xor wDH) And &HFF
        wDL = (wBL Xor wDL) And &HFF
        wCRC = wDL * 256 + wDH
    Next
    Compute_CRC = wCRC

End Function

Sub INIT_CRC(pCRC&())
    pCRC(0) = &H0&: pCRC(1) = &HC0C1&: pCRC(2) = &HC181&: pCRC(3) = &H140&
    pCRC(4) = &HC301&: pCRC(5) = &H3C0&: pCRC(6) = &H280&: pCRC(7) = &HC241&
    pCRC(8) = &HC601&: pCRC(9) = &H6C0&: pCRC(10) = &H780&: pCRC(11) = &HC741&

    pCRC(12) = &H500&: pCRC(13) = &HC5C1&: pCRC(14) = &HC481&: pCRC(15) = &H440&
    pCRC(16) = &HCC01&: pCRC(17) = &HCC0&: pCRC(18) = &HD80&: pCRC(19) = &HCD41&
    pCRC(20) = &HF00&: pCRC(21) = &HCFC1&: pCRC(22) = &HCE81&: pCRC(23) = &HE40&
    pCRC(24) = &HA00&: pCRC(25) = &HCAC1&: pCRC(26) = &HCB81&: pCRC(27) = &HB40&
    pCRC(28) = &HC901&: pCRC(29) = &H9C0&: pCRC(30) = &H880&: pCRC(31) = &HC841&
    pCRC(32) = &HD801&: pCRC(33) = &H18C0&: pCRC(34) = &H1980&: pCRC(35) = &HD941&:
    pCRC(36) = &H1B00&: pCRC(37) = &HDBC1&: pCRC(38) = &HDA81&: pCRC(39) = &H1A40&
    pCRC(40) = &H1E00&: pCRC(41) = &HDEC1&: pCRC(42) = &HDF81&: pCRC(43) = &H1F40&:
    pCRC(44) = &HDD01&: pCRC(45) = &H1DC0&: pCRC(46) = &H1C80&: pCRC(47) = &HDC41&
    pCRC(48) = &H1400&: pCRC(49) = &HD4C1&: pCRC(50) = &HD581&: pCRC(51) = &H1540&:
    pCRC(52) = &HD701&: pCRC(53) = &H17C0&: pCRC(54) = &H1680&: pCRC(55) = &HD641&
    pCRC(56) = &HD201&: pCRC(57) = &H12C0&: pCRC(58) = &H1380&: pCRC(59) = &HD341&:
    pCRC(60) = &H1100&: pCRC(61) = &HD1C1&: pCRC(62) = &HD081&: pCRC(63) = &H1040&
    pCRC(64) = &HF001&: pCRC(65) = &H30C0&: pCRC(66) = &H3180&: pCRC(67) = &HF141&:
    pCRC(68) = &H3300&: pCRC(69) = &HF3C1&: pCRC(70) = &HF281&: pCRC(71) = &H3240&
    pCRC(72) = &H3600&: pCRC(73) = &HF6C1&: pCRC(74) = &HF781&: pCRC(75) = &H3740&:
    pCRC(76) = &HF501&: pCRC(77) = &H35C0&: pCRC(78) = &H3480&: pCRC(79) = &HF441&
    pCRC(80) = &H3C00&: pCRC(81) = &HFCC1&: pCRC(82) = &HFD81&: pCRC(83) = &H3D40&:
    pCRC(84) = &HFF01&: pCRC(85) = &H3FC0&: pCRC(86) = &H3E80&: pCRC(87) = &HFE41&
    pCRC(88) = &HFA01&: pCRC(89) = &H3AC0&: pCRC(90) = &H3B80&: pCRC(91) = &HFB41&:
    pCRC(92) = &H3900&: pCRC(93) = &HF9C1&: pCRC(94) = &HF881&: pCRC(95) = &H3840&
    pCRC(96) = &H2800&: pCRC(97) = &HE8C1&: pCRC(98) = &HE981&: pCRC(99) = &H2940&:
    pCRC(100) = &HEB01&: pCRC(101) = &H2BC0&: pCRC(102) = &H2A80&: pCRC(103) = &HEA41&
    pCRC(104) = &HEE01&: pCRC(105) = &H2EC0&: pCRC(106) = &H2F80&: pCRC(107) = &HEF41&:
    pCRC(108) = &H2D00&: pCRC(109) = &HEDC1&: pCRC(110) = &HEC81&: pCRC(111) = &H2C40&
    pCRC(112) = &HE401&: pCRC(113) = &H24C0&: pCRC(114) = &H2580&: pCRC(115) = &HE541&:
    pCRC(116) = &H2700&: pCRC(117) = &HE7C1&: pCRC(118) = &HE681&: pCRC(119) = &H2640&
    pCRC(120) = &H2200&: pCRC(121) = &HE2C1&: pCRC(122) = &HE381&: pCRC(123) = &H2340&:
    pCRC(124) = &HE101&: pCRC(125) = &H21C0&: pCRC(126) = &H2080&: pCRC(127) = &HE041&
    pCRC(128) = &HA001&: pCRC(129) = &H60C0&: pCRC(130) = &H6180&: pCRC(131) = &HA141&:
    pCRC(132) = &H6300&: pCRC(133) = &HA3C1&: pCRC(134) = &HA281&: pCRC(135) = &H6240&
    pCRC(136) = &H6600&: pCRC(137) = &HA6C1&: pCRC(138) = &HA781&: pCRC(139) = &H6740&:
    pCRC(140) = &HA501&: pCRC(141) = &H65C0&: pCRC(142) = &H6480&: pCRC(143) = &HA441&
    pCRC(144) = &H6C00&: pCRC(145) = &HACC1&: pCRC(146) = &HAD81&: pCRC(147) = &H6D40&:
    pCRC(148) = &HAF01&: pCRC(149) = &H6FC0&: pCRC(150) = &H6E80&: pCRC(151) = &HAE41&
    pCRC(152) = &HAA01&: pCRC(153) = &H6AC0&: pCRC(154) = &H6B80&: pCRC(155) = &HAB41&:
    pCRC(156) = &H6900&: pCRC(157) = &HA9C1&: pCRC(158) = &HA881&: pCRC(159) = &H6840&
    pCRC(160) = &H7800&: pCRC(161) = &HB8C1&: pCRC(162) = &HB981&: pCRC(163) = &H7940&:
    pCRC(164) = &HBB01&: pCRC(165) = &H7BC0&: pCRC(166) = &H7A80&: pCRC(167) = &H8A41&
    pCRC(168) = &HBE01&: pCRC(169) = &H7EC0&: pCRC(170) = &H7F80&: pCRC(171) = &HBF41&:
    pCRC(172) = &H7D00&: pCRC(173) = &HBDC1&: pCRC(174) = &HBC81&: pCRC(175) = &H7C40&
    pCRC(176) = &HB401&: pCRC(177) = &H74C0&: pCRC(178) = &H7580&: pCRC(179) = &HB541&:
    pCRC(180) = &H7700&: pCRC(181) = &HB7C1&: pCRC(182) = &HB681&: pCRC(183) = &H7640&
    pCRC(184) = &H7200&: pCRC(185) = &HB2C1&: pCRC(186) = &HB381&: pCRC(187) = &H7340&:
    pCRC(188) = &HB101&: pCRC(189) = &H71C0&: pCRC(190) = &H7080&: pCRC(191) = &HB041&
    pCRC(192) = &H5000&: pCRC(193) = &H90C1&: pCRC(194) = &H9181&: pCRC(195) = &H5140&:
    pCRC(196) = &H9301&: pCRC(197) = &H53C0&: pCRC(198) = &H5280&: pCRC(199) = &H9241&
    pCRC(200) = &H9601&: pCRC(201) = &H56C0&: pCRC(202) = &H5780&: pCRC(203) = &H9741&:
    pCRC(204) = &H5500&: pCRC(205) = &H95C1&: pCRC(206) = &H9481&: pCRC(207) = &H5440&
    pCRC(208) = &H9C01&: pCRC(209) = &H5CC0&: pCRC(210) = &H5D80&: pCRC(211) = &H9D41&:
    pCRC(212) = &H5F00&: pCRC(213) = &H9FC1&: pCRC(214) = &H9E81&: pCRC(215) = &H5E40&
    pCRC(216) = &H5A00&: pCRC(217) = &H9AC1&: pCRC(218) = &H9B81&: pCRC(219) = &H5B40&:
    pCRC(220) = &H9901&: pCRC(221) = &H59C0&: pCRC(222) = &H5880&: pCRC(223) = &H9841&
    pCRC(224) = &H8801&: pCRC(225) = &H48C0&: pCRC(226) = &H4980&: pCRC(227) = &H8941&:
    pCRC(228) = &H4B00&: pCRC(229) = &H8BC1&: pCRC(230) = &H8A81&: pCRC(231) = &H4A40&
    pCRC(232) = &H4E00&: pCRC(233) = &H8EC1&: pCRC(234) = &H8F81&: pCRC(235) = &H4F40&:
    pCRC(236) = &H8D01&: pCRC(237) = &H4DC0&: pCRC(238) = &H4C80&: pCRC(239) = &H8C41&
    pCRC(240) = &H4400&: pCRC(241) = &H84C1&: pCRC(242) = &H8581&: pCRC(243) = &H4540&:
    pCRC(244) = &H8701&: pCRC(245) = &H47C0&: pCRC(246) = &H4680&: pCRC(247) = &H8641&
    pCRC(248) = &H8201&: pCRC(249) = &H42C0&: pCRC(250) = &H4380&: pCRC(251) = &H8341&:
    pCRC(252) = &H4100&: pCRC(253) = &H81C1&: pCRC(254) = &H8081&: pCRC(255) = &H4040&
End Sub
Compare Code (Text)
Generate Sort in VB or VBScript
 
Thanks for the suggestion Will. And thanks for the code, John. I'll try it out later this afternoon.
 
Init the array the easy way with
[tt]
DIM pCRC

pCRC = Array( _
&H0000&, &HC0C1&, &HC181&, &H140&, &HC301&, &H3C0&, &H280&, &HC241&, &HC601&, &H6C0&, _
&H0780&, &HC741&, &H0500&, &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&, &H8A41&, &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& )
[/tt]

Do data/read sets still work???

Wil Mead
wmead@optonline.net

 
Sorry, laziness took over. MY BAD!

I knew the continuation limits were being flirted with, get rid of the first continuation and last, that should be under 25 lines. Arrange in sets of 16 instead of 10... for only 16 lines etc.

Also figured that anyone concerned with performance would code a quick for loop forcing the values into a better object. Or make an init string and use a memory copy.

Wil Mead
wmead@optonline.net

 
Well, I tried that code out, and it seems, straightforward enough, if a bit hard to read. Unfortunately, it doesn't give me the checksum I need, but it's something to work with. I've looked over it, and I think I understand how the algorithm's working, but I'd like to know how you get the table. As I understand it, it's calculated from the polynomial somehow, but I have no clue what the procedure is. (Most of the web sites I've seen seem to gloss over that part.) Can you offer any insight?
 
There are many different CRC functions. The most common is the CCITT version, which was used in ZModem file transfers (there's a name from the distant past!).

The polynomial is:
[tab]X^16 + X^12 + X^5 + 1

and the guts of the function (in C) are:
[tab]crc = (unsigned char)(crc >> 8) | (crc << 8);
[tab]crc ^= X;
[tab]crc ^= (unsigned char)(crc & 0xff) >> 4;
[tab]crc ^= (crc << 8) << 4;
[tab]crc ^= ((crc & 0xff) << 4) << 1;

The ^= operator is the bit-wise Exclusive-Or. You can use VB's XOR function the same way, but since VB's integers are all signed (means they go from -32767 to +32768, instead of 0 to 65536 like C's unsigned char), you'll have to use Longs, and make sure to do an AND &hFFFF after ever operation.

Chip H.

 
I got it from &quot;Tech Journal&quot; back in the late '80s.
Found something like it (same constants) through Google.Com. Assembly language. Search hy &quot;CRC-16&quot; on .
ASM language.
&quot;This crc is calculated as X^16+X^15+X^2+1 and can
protect 2^16 bits or 8192 bytes against single bit
errors.&quot; Compare Code (Text)
Generate Sort in VB or VBScript
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top