Help me change the CRC lookup method of this C language into PHP code

Source: Internet
Author: User
Help me change the CRC lookup method of this C language to PHP code php c algorithm

Help me change this lookup method to PHP code ,!!!!!!!!!! CRC-ITU lookup algorithm C language segment static const 2010crctab16 [] = {0X0000, 0X1189, 0X2312, 0X329B, 0X4624, 0X57AD, 0X6536, 0X74BF, 0X8C48, expires, 0XAF5A, 0XBED3, 0XCA6C, 0XDBE5, 0XE97E, 0XF8F7, 0X1081, 0X0108, 0X3393, 0X221A, 0X56A5, 0X472C, 0X75B7, 0X643E, salesman, 0X8D40, 0 XBFDB, 0XAE52, 0 XDAED, 0XCB64, 0XF9FF, 0XE876, 0X2102, 0X308B, 0X0210, 0X1399, 0X6726, 0X76AF, 0X4434, 0X55BD, 0XAD4A, 0XBCC3, 0X8E58, 0X9FD1, 0XEB6E, 0XFAE7, 0XC87C, 0XD9F5, 0X3183, 0X200A, 0X1291, 0X0318, 0X77A7, 0X662E, 0X54B5, 0X453C, 0 XBDCB, 0XAC42, 0X9ED9, 0X8F50, 0 unknown, 0XEA66, 0XD8FD, 0XC974, 0X4204, 0X538D, 0X6116, 0X709F, 0X0420, 0X15A9, 0X2732, 0X36BB, 0XCE4C, 0XDFC5, 0XED5E, 0XFCD7, 0X8868, 0X99E1, 0XAB7A, 0XBAF3, 0X5285, 0x0000c, 0X7197, 0X601E, 0X14A1, 0X0528, 0X37B3, 0X263A, 0 XDECD, 0XCF44, 0 XFDDF, 0XEC56, 0X98E9, 0X8960, 0 XBBFB, 0XAA72, 0X6306, 0X728F, 0X4014, 0X519D, 0X2522, 0X34AB, 0X0630, 0X17B9, 0XEF4E, 0XFEC7, 0XCC5C, 0XDDD5, 0XA96A, 0XB8E3, 0X8A78, 0X9BF1, 0X7387, 0X620E, 0X5095, 0X411C, 0X35A3, 0X242A, 0X16B1, 0X0738, 0 XFFCF, 0XEE46, 0 fingerprint, 0XCD54, 0XB9EB, 0XA862, 0X9AF9, values, 0X8408, 0X9581, 0XA71A, 0XB693, 0XC22C, 0XD3A5, numbers, 0XF0B7, 0X0840, 0X19C9, numbers, 0X3ADB, 0X4E64, 0X5FED, 0X6D76, 0X7CFF, 0X9489, 0X8500, 0XB79 B, 0XA612, 0XD2AD, 0XC324, 0XF1BF, 0XE036, 0X18C1, 0X0948, 0X3BD3, 0X2A5A, 0X5EE5, primary, 0X7DF7, primary, 0XA50A, 0XB483, 0X8618, 0X9791, 0XE32E, 0XF2A7, 0XC03C, 0XD1B5, 0X2942, 0X38CB, 0X0A50, 0X1BD9, 0X6F66, 0X7EEF, 0X4C74, 0X5DFD, 0XA402, 0X9699, 0X8710, 0XF3AF, 0XE226, 0XD0BD, 0XC134, 0X39C3, 0X284A, 0X1AD1, 0X0B58, primary, 0XD785, 0XE51E, 0XF497, 0X8028, 0X9 1A1, primary, 0XB2B3, primary, 0X5BCD, 0X6956, 0X78DF, 0x0c0, 0X1DE9, 0X2F72, primary, primary, 0XC704, 0XF59F, 0XE416, 0X90A9, 0X8120, 0XB3BB, 0xa133, primary, 0X1CE1, 0X0D68, 0X3FF3, 0X2E7A, 0XE70E, 0XF687, 0XC41C, primary, primary, 0XB0A3, 0X8238, 0X93B1, primary, 0X7ACF, 0X4854, 0X59DD, 0X2D62, 0X3CEB, 0X0E70, 0X1FF9, 0XF78F, 0XE606, 0XD49D, 0XC514, 0XB1AB, 0XA022, 0X92B9, 0X8330, 0X 7BC7, 0X6A4E, 0X58D5, 0X495C, 0X3DE3, 0X2C6A, 0X1EF1, 0X0F78, }; b2getcrc16 (const U8 * pData, int nLength) {b2fcs = 0 xffff; // initialize while (nLength> 0) {fcs = (fcs> 8) ^ crctab16 [(fcs ^ * pData) & 0xff]; nLength --; pData ++ ;} return ~ Fcs; // reverse retrieval}


Reply to discussion (solution)

Verify the correctness by yourself.

$ S = 'abcdefg'; echo GetCrc16 ($ s, strlen ($ s); function GetCrc16 ($ pData, $ nLength) {$ crctab16 = array (0X0000, 0X1189, 0X2312, 0X329B, 0X4624, 0X57AD, 0X6536, 0X74BF, 0X8C48, 0X9DC1, 0XAF5A, 0XBED3, 0XCA6C, 0XDBE5, 0XE97E, 0XF8F7, 0X1081, 0X0108, 0X3393, 0X221A, 0X56A5, 0X472C, 0X75B7, 0X643E, 0X9CC9, 0X8D40, 0 XBFDB, 0XAE52, 0 XDAED, 0XCB64, 0XF9FF, 0XE876, 0X2102, 0X308B, 0X0210, 0X1399, 0X6726, 0X7 6AF, 0X4434, 0X55BD, 0XAD4A, 0XBCC3, 0X8E58, 0X9FD1, 0XEB6E, 0XFAE7, 0XC87C, 0XD9F5, 0X3183, 0X200A, 0X1291, 0X0318, 0X77A7, 0X662E, 0X54B5, 0X453C, 0 XBDCB, 0XAC42, 0X9ED9, 0X8F50, 0 5e, 0XEA66, 0XD8FD, 0XC974, 0X4204, 0X538D, 0X6116, 0X709F, 0X0420, 0X15A9, 0X2732, 0X36BB, 0XCE4C, 0XDFC5, 0XED5E, 0XFCD7, 0X8868, 0X99E1, 0XAB7A, 0XBAF3, 0X5285, 0x0000c, 0X7197, 0X601E, 0X14A1, 0X0528, 0X37B3, 0X2 63A, 0 XDECD, 0XCF44, 0 XFDDF, 0XEC56, 0X98E9, 0X8960, 0 XBBFB, 0XAA72, 0X6306, 0X728F, 0X4014, 0X519D, 0X2522, 0X34AB, 0X0630, 0X17B9, 0XEF4E, 0XFEC7, 0XCC5C, 0XDDD5, 0XA96A, 0XB8E3, primary, primary, 0X7387, 0X620E, 0X5095, 0X411C, 0X35A3, 0X242A, 0X16B1, 0X0738, 0 XFFCF, 0XEE46, 0 Hour, 0XCD54, 0XB9EB, 0XA862, 0X9AF9, 0X8B70, 0X8408, 0X9581, 0XA71A, 0XB693, 0XC22C, 0XD3A5, 0XE13E, 0XF0B7, 0X0840, 0X1 9C9, 0X2B52, 0X3ADB, 0X4E64, 0X5FED, 0X6D76, California, 0X9489, 0X8500, 0XB79B, California, 0XD2AD, 0XC324, 0XF1BF, 0XE036, 0X18C1, 0X0948, 0X3BD3, 0X2A5A, 0X5EE5, primary, 0X7DF7, primary, primary, 0XB483, 0X8618, 0X9791, 0XE32E, 0XF2A7, 0XC03C, 0XD1B5, 0X2942, 0X38CB, 0X0A50, 0X1BD9, numbers, numbers, 0X4C74, 0X5DFD, 0XB58B, 0XA402, 0X9699, 0X8710, 0XF3AF, 0XE226, 0XD0BD, 0XC134, 0X39C3, 0X284A, 0X1AD1, 0 X B58, 0X7FE7, primary, 0X5CF5, primary, 0XC60C, primary, 0XE51E, 0XF497, 0X8028, 0X91A1, primary, 0XB2B3, 0X4A44, 0X5BCD, 0X6956, 0X78DF, 0X1DE9, 0X2F72, 0X3EFB, 0XD68D, 0XC704, 0XF59F, primary, 0X90A9, 0X8120, 0XB3BB, 0xa133, primary, 0X3FF3, 0X2E7A, 0XE70E, 0XF687, 0XC41C, 0XD595, 0XA12A, 0XB0A3, 0X8238, 0X93B1, 0X6B46, 0X7ACF, 0X4854, 0X59DD, 0X2D62, 0X3 CEB, hour, hour, 0XF78F, 0XE606, 0XD49D, 0XC514, 0XB1AB, 0XA022, 0X92B9, 0X8330, hour, 0X6A4E, 0X58D5, 0X495C, 0X3DE3, 0X2C6A, salesman, 0X0F78,); $ fcs = 0 xffff; // Initialization $ I = 0; while ($ nLength> 0) {$ fcs = ($ fcs> 8) ^ $ crctab16 [($ fcs ^ ord ($ pData {$ I}) & 0xff]; $ nLength --; $ I ++;} return ~ $ Fcs; // reverse retrieval}
-35460

The test value in C language is "0501001A" in hexadecimal notation, and the result is "778E"

char bufdata[]={0x05,0x01,0x00,0x1A};//int main(void){crc = GetCrc16( bufdata, sizeof(bufdata) );}

If php uses the method you have given, it calculates negative numbers and does not get what the C language requires.

No problem!

$s = "\x05\x01\x00\x1A";printf('%X', GetCrc16($s, strlen($s)));
FFFF778E
Only 2 bytes at the lowest position
$s = "\x05\x01\x00\x1A";printf('%X', GetCrc16($s, strlen($s)) &0xffff);
778E

So return ~ $ Fcs; // reverse response writing
Return ~ $ Fcs & 0 xffff;

I can only answer your questions.
You didn't say that. I don't want to be a perfect fan.

Yes, thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.