Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
#include<stdio.h>
#include<conio.h>

int main(short argc, char** argv)
{

   unsigned char serNum [] = "DS0008D-0111A";
   unsigned char *ser = serNum;
   unsigned short checksum;
checksum = CalcChksum(*ser,13);
printf("the checksum is %x",checksum)  ;
return 0;
}


unsigned short CalcChksum(unsigned char *ptr,int bytecnt)
{
   int i;
   unsigned short chksum;

   chksum = 0;
   // Calculate 1's complement chksum
   for(i=0; i<bytecnt; i++)
   {
   chksum += (unsigned short)*ptr;
   chksum = (char)~chksum;
   }
   return(chksum);

}
Posted
Comments
Legor 17-Oct-14 3:24am    
Mind formulating a question?

1 solution

First of all I think the method CalcChksum needs to appear above your main. Or you'll at least need to forward-declare it.

Other than that, calling CalcChksum like this;
C
unsigned char *ser = serNum;
checksum = CalcChksum(*ser,13);

Is incorrect, CalcChksum is declared like;
C
unsigned short CalcChksum(unsigned char *ptr, int bytecnt)

So the first parameter should be a char*, by putting as asterisk in front of ser you're de-referencing that from an unsigned char * to a unsigned char.
Change the call to
C
checksum = CalcChksum(ser,13);

Hope this helps,
Fredrik
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900