0

here i have confusion in output of CRC16.

Here first i used only one function which using char readChar and counts CRC16 of any type of file but in .tar or .tar.gz files i got some problem regarding EOF and in my previous question some one suggested me use int instead of char in declartion of readChar.

So After this i got solution of EOF in while loop and its read proper bytes.

Than After here i put two methods for calculating CRC16. In first one i used int readChar and in second one i used char readChar. So i got different out for same file this problem i am getting in .tar and .tar.gz files only. In simple .txt files this both methods gives same result.

user1089679
  • 2,328
  • 8
  • 41
  • 51
  • Here's what I would suggest: Instead of trying this with a 400 megabyte input file (!), try with an input file of about 10 characters or so. Then, you can print out debugging information and compare the two runs. – Greg Hewgill Feb 24 '12 at 05:16
  • @GregHewgill Thanx For Your Reply . i already checked with 10 charcters and its given perfect same result in both cases – user1089679 Feb 24 '12 at 05:23
  • Try open the file in binary read mode (use "rb" argument for open). – alexander Feb 24 '12 at 09:17

2 Answers2

1

It looks like the problem might be the use of signed characters. Try explicitly declaring your character as unsigned:

unsigned char readChar;

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 calculations.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • Thanks i will try and let you knw – user1089679 Feb 24 '12 at 05:34
  • Ya your suggestion is correct and i got same results in both program. Now can u please tell me Which program i will use for CRC16 calculation.? int or unsigned char ? – user1089679 Feb 24 '12 at 05:40
  • If both implementations give the same answer, then you may wish to choose based on other factors. For example, you may wish to choose the fastest implementation. – Greg Hewgill Feb 24 '12 at 09:27
  • Thanks A lot for Your reply i changed it to unsigned char and now can u please refer my next question where i put my next query in detail. http://stackoverflow.com/questions/9428468/which-datatype-is-better-in-calculation-of-crc16-for-any-type-of-file – user1089679 Feb 24 '12 at 09:39
0

Why do you think your answer is wrong? What flavor of CRC16 are you trying to calculate? What tool are you comparing your result against?

You can get rid of the char version. getc() returns an unsigned char value inside an integer (or the integer value EOF), it's wrong to assign it to a char.

It looks like you assume shorts are 16 bits long. Did you verify this?

You should always work with all compiler warnings enabled. You have unused variables, uninitialized variables, undeclared functions and missing parameters to function calls. I'm amazed your program works at all; it crashed on my computer.

aib
  • 45,516
  • 10
  • 73
  • 79
  • Thanks For Your Response but i will remove this unused and uninitialized variables later. first i want to solve this thing. i used here short for type casting after readChar assigned – user1089679 Feb 24 '12 at 05:37
  • Can u please Suggest me which one is Better i int datatype function or unsgined char data type function? – user1089679 Feb 24 '12 at 05:46