2012-02-29 85 views
2

我有下面的C代碼,我試圖用java重寫。 我希望在他們兩個看到類似的輸出,但我得到不同的輸出。 這是計算校驗和。校驗和問題CRC16CCITT

這裏是C代碼:

#include <ctype.h> 
#include <string.h> 
#include <stdio.h> 
/*~+:CRC Table*/ 
static unsigned short crctab[256] = 
{ 
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, 
0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF, 
0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6, 
0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE, 
0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485, 
0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D, 
0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4, 
0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC, 
0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823, 
0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B, 
0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12, 
0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A, 
0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41, 
0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49, 
0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70, 
0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78, 
0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F, 
0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067, 
0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E, 
0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256, 
0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D, 
0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, 
0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C, 
0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634, 
0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB, 
0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3, 
0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A, 
0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92, 
0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9, 
0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1, 
0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8, 
0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0 
}; 

void convert_toASCII (char *buffer, int value) 
{ 
/* 
Function converts given 'value' into a 4 byte 
ASCII-HEX-String (with leading zeros) to 'buffer[]' 
Parameter: char *buffer, int value 
Returns: none 
*/ 
static unsigned char hex_num[] = "ABCDEF"; 
static unsigned char i; /* HV */ 
i = (char)((value & 0xf000) >> 12); 
*buffer = (char)(hex_num[i]); 
++buffer; 
i = (char)((value & 0x0f00) >> 8); 
*buffer = (char)(hex_num[i]); 
++buffer; 
i = (char)((value & 0x00f0) >> 4); 
*buffer = (char)(hex_num[i]); 
++buffer; 
i = (char)(value & 0x000f); 
*buffer = (char)(hex_num[i]); 
} 

void calc_crc(unsigned char *databuffer,unsigned int length) 
{ 
/*~+:Modulname: calc_crc */ 
/*~+:Calculate serial CRC (according CCITT) */ 
/*~+:The serial CRC16 is calculated for a certain length (int length) */ 
/*~+:over bytes in buffer (char databuffer[]) */ 
/*~+: */ 
/*~+:Input: *databuffer pointer to data string */ 
/*~+: laenge number of chars to build CRC for */ 
/*~+:Output: the CRC will be added in ASCII characters (4 chars) */ 
/*~+: at the end of the given string and terminated with '\0' */ 
/*~+: The buffer must be able to handle these additional */ 
/*~+: 5 characters */ 
static unsigned char tmp; 
static unsigned int crc,zaehler; 
crc = 0; 
for (zaehler = 0;zaehler < length ;zaehler ++) 
{ 
tmp=(unsigned char) (crc>>8) ; 
crc=(crc<<8)^crctab[tmp]^*databuffer; 
databuffer++; 
} 

printf("%u", crc); 


/* convert crc -> ASCII */ 
/* append to string */ 
convert_toASCII (databuffer, crc); 
} 

void main(void) 
{ 
static char Data[] = {"abcdefghij"}; 
static char buffer[64]; 
strcpy(buffer,Datensatz); 
printf("Data : %s \n\r",&buffer[0]); 
calc_crc(buffer,10); 
printf("CRC : %s \n\r",&buffer[10]); 
printf("Data mit CRC: %s \n\r",&buffer[0]); 
} 

我寫的Java代碼:

public final class Checksum 
{ 
    public static void main(final String[] args) 
    { 
     final String checksumString = "abcdefghij"; 
     final int checksum = calculateCRC16CCITTChecksum(checksumString); 
     System.out.println("Checksum integer value:" + checksum); 
     System.out.println("Checksum value in Hex:" + Integer.toHexString(checksum)); 
    } 

    /** 
    * @param frame The frame for whose checksum has to be calculated. 
    * @return The calculated checksum. 
    */ 
    private final static int calculateCRC16CCITTChecksum(final String frame) 
    { 
     final int[] CRC16_Lookup = {0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF, 0x1231, 0x0210, 0x3273, 0x2252, 
       0x52B5, 0x4294, 0x72F7, 0x62D6, 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE, 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485, 0xA56A, 0xB54B, 0x8528, 
       0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D, 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4, 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC, 0x48C4, 0x58E5, 
       0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823, 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B, 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12, 0xDBFD, 
       0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A, 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41, 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49, 
       0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70, 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78, 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 
       0xE16F, 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067, 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E, 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 
       0x6277, 0x7256, 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D, 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 
       0xF77E, 0xC71D, 0xD73C, 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634, 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB, 0x5844, 0x4865, 0x7806, 0x6827, 
       0x18C0, 0x08E1, 0x3882, 0x28A3, 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A, 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92, 0xFD2E, 0xED0F, 0xDD6C, 
       0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9, 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1, 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8, 0x6E17, 0x7E36, 
       0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0}; 
     // check sum for polynomial 1.x16 + 0.x15 + 0.x14 + 0.x13 + 1.x12 + 0.x11 + 0.x10 + 0.x9 + 0.x8 + 0.x7 + 0.x6 + 1.x5 + 0.x4 + 0.x3 + 0.x2 +0.x1 + 1.x0. 
     // 1.x16 - implies 1 multiplied by 'x' to the power of 16. 
     int crc = 0; 

     for (int i = 0, size = frame.length(); i < size; i++) 
     { 
      crc = (crc << 8)^CRC16_Lookup[(crc >> 8) & 0xFF]^(frame.charAt(i) & 0xFF); 
     } 

     return crc & 0xFFFF; 
    } 
} 

整數值輸出,我在Java代碼中得到的是從十進制什麼不同我在C中獲得的值,即使在java中的十六進制字符串轉換產生與C中的十六進制轉換不同的結果。 請指導我做錯了什麼。

感謝您的期待!

+0

哪個實現產生*正確* CRC? (例如,用*已知好的*來源測試)。我建議在兩個版本中對齊表格(C看起來更好,8個值/行,IMOHO),然後對它們做一個DIFF。另外,考慮使用動態生成的表格...這很容易。 – 2012-02-29 22:56:54

+1

@Rishi:我不經常調試調試器,但這是一種情況,我可以通過兩個併發的調試器,一個用於Java,另一個用於C,並通過一步一步地調試來獲得樂趣。看看發生了什麼; ) – TacticalCoder 2012-03-01 00:09:56

+0

據我所知,Java中的所有基元都被簽名 - 所以也許這只是一個表示問題? – HectorLector 2012-03-01 00:16:49

回答

0

這不是嚴格與實施問題有關,但我想這是值得一提的。請記住,某些通信協議要求將輸入值和輸出值與某個值異或,更不用說反映輸入數據的位或字節。這發生在以太網中,它使用CRC-32進行實際計算,但輸入和輸出數據與FF..FF異或(因此它被註釋),並且輸入字節中的所有位(或者更自然地,半字節)被反映。記住這一點 - 實際的計算可能是正確的,但是可能存在一些你根本不知道的數據修改方面的問題,導致完全不同的結果。

+1

downvoter會關注評論嗎? – 2012-03-01 00:39:01

+0

Upvoted這個無法解釋的downvote。答案是完全正確的。它可能已經清楚地表明,這是通過將初始CRC值從0改爲0xffff來實現的。 – EJP 2012-03-01 01:33:39

+0

感謝丹尼爾的寶貴意見。 – Rishi 2012-03-01 03:54:45

0

通過代碼檢查,可以看到在Java中CRC被限制爲16位,然後以十進制打印(在返回函數之前使用& 0xFFFF)。在C中,打印的值是完整的無符號整數,所以應用相同的掩碼應該可以實現。

我不明白爲什麼十六進制值不同,但...

+0

感謝您的輸入。我得到了與我的代碼相同的值,這只是我錯過了輸入字符串中的一些字符到Java代碼的時候與C相比。 – Rishi 2012-03-01 14:46:51