title: Which datatype is better in calculation of CRC16 for any type of file
body: <p>Here i am using two different functions for calculating CRC16 for any type of file <code>(.txt,.tar,.tar.gz,.bin,.scr,.sh etc)</code> and different size also varies from <code>1 KB to 5 GB</code>.</p>
  
  <p>I want to achieve this </p>
  
  <pre><code>   `cross platform 
  
     less time consuming
  
     Have to work proper for any type of file and any size`
  </code></pre>
  
  <p>i got same value of CRC in both functions. but any one can tell me which one is more better to calculate CRC16 for any type of file with any size on different different platform.</p>
  
  <p>Here we have to consider 0 to 255 all type characters.</p>
  
  <p>Can any body please suggest me which one is good in my requirements.</p>
  
  <p>Code of both functions : </p>
  
  <p>First one which has <code>int</code> datatype in <code>readChar</code> here i am using <code>int readChar</code></p>
  
  <pre><code>int CRC16_int(const char* filePath) {
  
      //Declare variable to store CRC result.
      unsigned short result;
      //Declare loop variables.
      int intInnerLoopIndex;
      result = 0xffff; //initialize result variable to perform CRC checksum calculation.
  
      //Store message which read from file.
      //char content[2000000];
  
      //Create file pointer to open and read file.
      FILE *readFile;
  
      //Use to read character from file.
      int readChar;
  
      //open a file for Reading
      readFile = fopen(filePath, "rb");
  
      //Checking file is able to open or exists.
      if (!readFile) {
          fputs("Unable to open file %s", stderr);
      }
      /*
       Here reading file and store into variable.
       */
      int chCnt = 0;
      while ((readChar = getc(readFile)) != EOF) {
  
          //printf("charcater is %c\n",readChar);
          //printf("charcater is %c and int is %d \n",readChar,readChar);
          result ^= (short) (readChar);
          for (intInnerLoopIndex = 0; intInnerLoopIndex &lt; 8; intInnerLoopIndex++) {
              if ((result &amp; 0x0001) == 0x0001) {
                  result = result &gt;&gt; 1; //Perform bit shifting.
                  result = result ^ 0xa001; //Perform XOR operation on result.
              } else {
                  result = result &gt;&gt; 1; //Perform bit shifting.
              }
          }
  
          //content[chCnt] = readChar;
          chCnt++;
      }
      printf("\nCRC data length in file: %d", chCnt);
      //This is final CRC value for provided message.
      return (result);
  }
  </code></pre>
  
  <p>Second one is <code>unsigned char</code> datatype of <code>readChar</code> Here i am using <code>unsigned char readChar</code></p>
  
  <pre><code>int CRC16_unchar(const char* filePath) {
  
      unsigned int filesize;
      //Declare variable to store CRC result.
      unsigned short result;
      //Declare loop variables.
      unsigned int intOuterLoopIndex, intInnerLoopIndex;
      result = 0xffff; //initialize result variable to perform CRC checksum calculation.
      FILE *readFile;
      //Use to read character from file.
      //The problem is if you read a byte from a file with the hex value (for example) 0xfe, 
      //then the char value will be -2 while the unsigned char value will be 254. 
      //This will significantly affect your CRC 
      unsigned char readChar;
      //open a file for Reading
      readFile = fopen(filePath, "rb");
      //Checking file is able to open or exists.
      if (!readFile) {
          fputs("Unable to open file %s", stderr);
      }
      fseek(readFile, 0, SEEK_END); // seek to end of file
      filesize = ftell(readFile); // get current file pointer
      fseek(readFile, 0, SEEK_SET); // seek back to beginning of file
      /*
       Here reading file and store into variable.
       */
      int chCnt = 0;
  
      for (intOuterLoopIndex = 0; intOuterLoopIndex &lt; filesize; intOuterLoopIndex++) {
          readChar = getc(readFile);
          printf("charcater is %c and int is %d\n",readChar,readChar);
  
                  result ^= (short) (readChar);
                  for (intInnerLoopIndex = 0; intInnerLoopIndex &lt; 8; intInnerLoopIndex++) {
                      if ((result &amp; 0x0001) == 0x0001) {
                          result = result &gt;&gt; 1; //Perform bit shifting.
                          result = result ^ 0xa001; //Perform XOR operation on 
                      } else {
                          result = result &gt;&gt; 1; //Perform bit shifting.
                      }
                  }
                  chCnt++;
      }
      printf("\nCRC data length in file: %d", chCnt);
      return (result);
  }
  </code></pre>
  
  <p>Please Help me to figure out this problem</p>
  
  <p>Thanks</p>