-- 作者:有点甜
-- 发布时间:2014/12/22 19:16:00
--
全局代码
Public CRC16Table As UShort() = {&H0, &H1021, &H2042, &H3063, &H4084, &H50a5, _ &H60c6, &H70e7, &H8108, &H9129, &Ha14a, &Hb16b, _ &Hc18c, &Hd1ad, &He1ce, &Hf1ef, &H1231, &H210, _ &H3273, &H2252, &H52b5, &H4294, &H72f7, &H62d6, _ &H9339, &H8318, &Hb37b, &Ha35a, &Hd3bd, &Hc39c, _ &Hf3ff, &He3de, &H2462, &H3443, &H420, &H1401, _ &H64e6, &H74c7, &H44a4, &H5485, &Ha56a, &Hb54b, _ &H8528, &H9509, &He5ee, &Hf5cf, &Hc5ac, &Hd58d, _ &H3653, &H2672, &H1611, &H630, &H76d7, &H66f6, _ &H5695, &H46b4, &Hb75b, &Ha77a, &H9719, &H8738, _ &Hf7df, &He7fe, &Hd79d, &Hc7bc, &H48c4, &H58e5, _ &H6886, &H78a7, &H840, &H1861, &H2802, &H3823, _ &Hc9cc, &Hd9ed, &He98e, &Hf9af, &H8948, &H9969, _ &Ha90a, &Hb92b, &H5af5, &H4ad4, &H7ab7, &H6a96, _ &H1a71, &Ha50, &H3a33, &H2a12, &Hdbfd, &Hcbdc, _ &Hfbbf, &Heb9e, &H9b79, &H8b58, &Hbb3b, &Hab1a, _ &H6ca6, &H7c87, &H4ce4, &H5cc5, &H2c22, &H3c03, _ &Hc60, &H1c41, &Hedae, &Hfd8f, &Hcdec, &Hddcd, _ &Had2a, &Hbd0b, &H8d68, &H9d49, &H7e97, &H6eb6, _ &H5ed5, &H4ef4, &H3e13, &H2e32, &H1e51, &He70, _ &Hff9f, &Hefbe, &Hdfdd, &Hcffc, &Hbf1b, &Haf3a, _ &H9f59, &H8f78, &H9188, &H81a9, &Hb1ca, &Ha1eb, _ &Hd10c, &Hc12d, &Hf14e, &He16f, &H1080, &Ha1, _ &H30c2, &H20e3, &H5004, &H4025, &H7046, &H6067, _ &H83b9, &H9398, &Ha3fb, &Hb3da, &Hc33d, &Hd31c, _ &He37f, &Hf35e, &H2b1, &H1290, &H22f3, &H32d2, _ &H4235, &H5214, &H6277, &H7256, &Hb5ea, &Ha5cb, _ &H95a8, &H8589, &Hf56e, &He54f, &Hd52c, &Hc50d, _ &H34e2, &H24c3, &H14a0, &H481, &H7466, &H6447, _ &H5424, &H4405, &Ha7db, &Hb7fa, &H8799, &H97b8, _ &He75f, &Hf77e, &Hc71d, &Hd73c, &H26d3, &H36f2, _ &H691, &H16b0, &H6657, &H7676, &H4615, &H5634, _ &Hd94c, &Hc96d, &Hf90e, &He92f, &H99c8, &H89e9, _ &Hb98a, &Ha9ab, &H5844, &H4865, &H7806, &H6827, _ &H18c0, &H8e1, &H3882, &H28a3, &Hcb7d, &Hdb5c, _ &Heb3f, &Hfb1e, &H8bf9, &H9bd8, &Habbb, &Hbb9a, _ &H4a75, &H5a54, &H6a37, &H7a16, &Haf1, &H1ad0, _ &H2ab3, &H3a92, &Hfd2e, &Hed0f, &Hdd6c, &Hcd4d, _ &Hbdaa, &Had8b, &H9de8, &H8dc9, &H7c26, &H6c07, _ &H5c64, &H4c45, &H3ca2, &H2c83, &H1ce0, &Hcc1, _ &Hef1f, &Hff3e, &Hcf5d, &Hdf7c, &Haf9b, &Hbfba, _ &H8fd9, &H9ff8, &H6e17, &H7e36, &H4e55, &H5e74, _ &H2e93, &H3eb2, &Hed1, &H1ef0}
Public Function CRC16(dat As [Byte](), count As Integer) As Integer Dim crc As Integer = 0 Dim crctemp As [Byte] Dim i As Integer = 0 While count > 0 count -= 1 crctemp = Convert.ToByte(crc >> 8) crc = (crc << 8) And &Hffff crc = (CRC16Table(crctemp Xor dat(i)) Xor crc) And &Hffff i += 1 End While Return crc End Function
调用代码
Dim aaff As [Byte]() = {&H01, &H04, &H00, &H00, &H00, &H0a} Dim aa As Integer = CRC16(aaff, aaff.Length) Dim bb As String = Convert.ToString(aa, 16).ToUpper() msgbox(bb)
|
-- 作者:111yangguojun
-- 发布时间:2014/12/23 17:37:00
--
CRC16 计算方法【查表法】:使用2 个256 长度的校验表 // -------------------------------------------------------------- // CRC 高位字节值表 //--------------------------------------------------------------- const BYTE chCRCHTalbe[] = { 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 }; // -------------------------------------------------------------- // CRC 低位字节值表 //--------------------------------------------------------------- const BYTE chCRCLTalbe[] = { 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, 0x40 }; // -------------------------------------------------------------- // CRC 算法子函数 //--------------------------------------------------------------- WORD CRC16_1(BYTE* pchMsg, WORD wDataLen) { BYTE chCRCHi = 0xFF; // 高CRC 字节初始化 BYTE chCRCLo = 0xFF; // 低CRC 字节初始化 WORD wIndex; // CRC 循环中的索引 while (wDataLen--) { // 计算CRC wIndex = chCRCLo ^ *pchMsg++ ; chCRCLo = chCRCHi ^ chCRCHTalbe[wIndex]; chCRCHi = chCRCLTalbe[wIndex] ; } return ((chCRCHi << 8) | chCRCLo) ; }
这个是 上面数据与校验码的算法,不清楚如何转换成狐表的代码,看着都眼花缭乱的
|
-- 作者:有点甜
-- 发布时间:2014/12/23 18:03:00
--
全局代码
Public chCRCHTalbe As UInteger() = {&H0, &Hc1, &H81, &H40, &H1, &Hc0, _ &H80, &H41, &H1, &Hc0, &H80, &H41, _ &H0, &Hc1, &H81, &H40, &H1, &Hc0, _ &H80, &H41, &H0, &Hc1, &H81, &H40, _ &H0, &Hc1, &H81, &H40, &H1, &Hc0, _ &H80, &H41, &H1, &Hc0, &H80, &H41, _ &H0, &Hc1, &H81, &H40, &H0, &Hc1, _ &H81, &H40, &H1, &Hc0, &H80, &H41, _ &H0, &Hc1, &H81, &H40, &H1, &Hc0, _ &H80, &H41, &H1, &Hc0, &H80, &H41, _ &H0, &Hc1, &H81, &H40, &H1, &Hc0, _ &H80, &H41, &H0, &Hc1, &H81, &H40, _ &H0, &Hc1, &H81, &H40, &H1, &Hc0, _ &H80, &H41, &H0, &Hc1, &H81, &H40, _ &H1, &Hc0, &H80, &H41, &H1, &Hc0, _ &H80, &H41, &H0, &Hc1, &H81, &H40, _ &H0, &Hc1, &H81, &H40, &H1, &Hc0, _ &H80, &H41, &H1, &Hc0, &H80, &H41, _ &H0, &Hc1, &H81, &H40, &H1, &Hc0, _ &H80, &H41, &H0, &Hc1, &H81, &H40, _ &H0, &Hc1, &H81, &H40, &H1, &Hc0, _ &H80, &H41, &H1, &Hc0, &H80, &H41, _ &H0, &Hc1, &H81, &H40, &H0, &Hc1, _ &H81, &H40, &H1, &Hc0, &H80, &H41, _ &H0, &Hc1, &H81, &H40, &H1, &Hc0, _ &H80, &H41, &H1, &Hc0, &H80, &H41, _ &H0, &Hc1, &H81, &H40, &H0, &Hc1, _ &H81, &H40, &H1, &Hc0, &H80, &H41, _ &H1, &Hc0, &H80, &H41, &H0, &Hc1, _ &H81, &H40, &H1, &Hc0, &H80, &H41, _ &H0, &Hc1, &H81, &H40, &H0, &Hc1, _ &H81, &H40, &H1, &Hc0, &H80, &H41, _ &H0, &Hc1, &H81, &H40, &H1, &Hc0, _ &H80, &H41, &H1, &Hc0, &H80, &H41, _ &H0, &Hc1, &H81, &H40, &H1, &Hc0, _ &H80, &H41, &H0, &Hc1, &H81, &H40, _ &H0, &Hc1, &H81, &H40, &H1, &Hc0, _ &H80, &H41, &H1, &Hc0, &H80, &H41, _ &H0, &Hc1, &H81, &H40, &H0, &Hc1, _ &H81, &H40, &H1, &Hc0, &H80, &H41, _ &H0, &Hc1, &H81, &H40, &H1, &Hc0, _ &H80, &H41, &H1, &Hc0, &H80, &H41, _ &H0, &Hc1, &H81, &H40}
Public chCRCLTalbe As Uinteger() = {&H0, &Hc0, &Hc1, &H1, &Hc3, &H3, _ &H2, &Hc2, &Hc6, &H6, &H7, &Hc7, _ &H5, &Hc5, &Hc4, &H4, &Hcc, &Hc, _ &Hd, &Hcd, &Hf, &Hcf, &Hce, &He, _ &Ha, &Hca, &Hcb, &Hb, &Hc9, &H9, _ &H8, &Hc8, &Hd8, &H18, &H19, &Hd9, _ &H1b, &Hdb, &Hda, &H1a, &H1e, &Hde, _ &Hdf, &H1f, &Hdd, &H1d, &H1c, &Hdc, _ &H14, &Hd4, &Hd5, &H15, &Hd7, &H17, _ &H16, &Hd6, &Hd2, &H12, &H13, &Hd3, _ &H11, &Hd1, &Hd0, &H10, &Hf0, &H30, _ &H31, &Hf1, &H33, &Hf3, &Hf2, &H32, _ &H36, &Hf6, &Hf7, &H37, &Hf5, &H35, _ &H34, &Hf4, &H3c, &Hfc, &Hfd, &H3d, _ &Hff, &H3f, &H3e, &Hfe, &Hfa, &H3a, _ &H3b, &Hfb, &H39, &Hf9, &Hf8, &H38, _ &H28, &He8, &He9, &H29, &Heb, &H2b, _ &H2a, &Hea, &Hee, &H2e, &H2f, &Hef, _ &H2d, &Hed, &Hec, &H2c, &He4, &H24, _ &H25, &He5, &H27, &He7, &He6, &H26, _ &H22, &He2, &He3, &H23, &He1, &H21, _ &H20, &He0, &Ha0, &H60, &H61, &Ha1, _ &H63, &Ha3, &Ha2, &H62, &H66, &Ha6, _ &Ha7, &H67, &Ha5, &H65, &H64, &Ha4, _ &H6c, &Hac, &Had, &H6d, &Haf, &H6f, _ &H6e, &Hae, &Haa, &H6a, &H6b, &Hab, _ &H69, &Ha9, &Ha8, &H68, &H78, &Hb8, _ &Hb9, &H79, &Hbb, &H7b, &H7a, &Hba, _ &Hbe, &H7e, &H7f, &Hbf, &H7d, &Hbd, _ &Hbc, &H7c, &Hb4, &H74, &H75, &Hb5, _ &H77, &Hb7, &Hb6, &H76, &H72, &Hb2, _ &Hb3, &H73, &Hb1, &H71, &H70, &Hb0, _ &H50, &H90, &H91, &H51, &H93, &H53, _ &H52, &H92, &H96, &H56, &H57, &H97, _ &H55, &H95, &H94, &H54, &H9c, &H5c, _ &H5d, &H9d, &H5f, &H9f, &H9e, &H5e, _ &H5a, &H9a, &H9b, &H5b, &H99, &H59, _ &H58, &H98, &H88, &H48, &H49, &H89, _ &H4b, &H8b, &H8a, &H4a, &H4e, &H8e, _ &H8f, &H4f, &H8d, &H4d, &H4c, &H8c, _ &H44, &H84, &H85, &H45, &H87, &H47, _ &H46, &H86, &H82, &H42, &H43, &H83, _ &H41, &H81, &H80, &H40}
Public Function CRC16(pchMsg As uinteger(), wDataLen As ushort) As ushort Dim chCRCHi As uinteger = &Hff \' 高CRC 字节初始化 Dim chCRCLo As uinteger = &Hff \' 低CRC 字节初始化 Dim wIndex As ushort Dim i As ushort = 0 \' CRC 循环中的索引 While wDataLen > 0 \' 计算CRC wIndex = chCRCLo Xor pchMsg(i) i += 1 chCRCLo = chCRCHi Xor chCRCHTalbe(wIndex) chCRCHi = chCRCLTalbe(wIndex) wDataLen -= 1 End While Return ((chCRCHi << 8) Or chCRCLo)
End Function
[此贴子已经被作者于2018/11/16 10:43:10编辑过]
|