I'm a .net programmer so forgive me if this is a stupid question, but here goes. I'm working with a vendor to interface our system with theirs. We are sending text data via modem. The protocol specifications they have given us require a checksum value to be appended to the text sent. In the specifications they have included the code to calculate the checksum and also state that the checksum of XYZ should equate to 470f. I get DD7A for the string XYZ. I'm not real familiar with C code so I may be doing something completely wrong. When i call the test_crc method in the below code I don't not get 470f. Also, why does the test_crc method have two parameters when they don't seem to be used. Please help!!!

#define CRCRES 0xf0b8  /* residue for good verify */ 
#define DEBUG 
  
unsigned crctbl[] = {0x0000, 0x1081, 0x2102, 0x3183, 
                     0x4204, 0x5285, 0x6306, 0x7387, 
                     0x8408, 0x9489, 0xa50a, 0xb58b, 
                     0xc60c, 0xd68d, 0xe70e, 0xf78f}; 

/* 
 * This uses a 32 byte table to lookup the crc 4 bits at a time. 
 * The CRC CCITT is used. 
 */ 
  
  
unsigned short calc_crc(unsigned char *ptr, unsigned length) 
  
{ 
  unsigned short crc; 
  unsigned short i; 
  unsigned char pos,ch; 
  
  crc = 0xffff;  /* precondition crc */ 
  for (i = 0; i < length; i++,ptr++) { 
    ch = *ptr; 
    pos = (crc ^ ch) & 15; 
    crc = ((crc >> 4) & 0x0fff) ^ crctbl[pos]; 
    ch >>= 4; 
    pos = (crc^ch) & 15; 
    crc = ((crc >> 4) & 0xffff) ^ crctbl[pos]; 
  } 
  crc = ~crc;  /* post condition */ 
  crc = (crc << 8) | (crc >> 8); /* bytewise reverse */ 
  return crc; 
} 


/* 
 * tests the block of code containing the crc to verify it's 
 * content.  This compares to the reversed and inverted 
 * residue. 
 */ 
  
int test_crc(unsigned char *ptr, unsigned length) 
  
{ 
  unsigned short crc; 
  unsigned char arr [] = {'X','Y','Z'}; 

  crc = calc_crc(arr,3); 
  printf("Calced crc of test to be %04x, (should be 470f)\n", crc); 
  return (crc == 0x470f); 
}

Recommended Answers

All 4 Replies

The code looks reasonable, except that "and"ing with 0x0fff and 0xffff does nothing in this situation (assuming 16-bit shorts). Removing those will change nothing, however. I would check the "answer" you've been given before going any further. Your code may be just fine.

As for test_crc, there is no reason for those params.

The code looks reasonable, except that "and"ing with 0x0fff and 0xffff does nothing in this situation (assuming 16-bit shorts). Removing those will change nothing, however. I would check the "answer" you've been given before going any further. Your code may be just fine.

As for test_crc, there is no reason for those params.

You were correct. The "answer" they had given in test_crc was incorrect. DD7A was the correct checksum for "XYZ".

Thanks for you help.

What is ptr here?
it it a string or a characted?please explain with example...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.