Calculate checksum for a byte array

Hello all,
i want to write a code for a method which takes a byte array and perform the checksum for all the elements of array and then add the result of the checksum in the last position of that array.tell me what will the process and how can i do that.. Any example will be quite helpful. thank you

duhit Choudhary wrote:i want to write a code for a method which takes a byte array and perform the checksum for all the elements of array and then add the result of the checksum in the last position of that array.tell me what will the process and how can i do that.. Any example will be quite helpful. thank you

Simplest one I can think of for a byte checksum:but it's probably horribly flawed. Some variant, maybe involving an index might be better.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here

You can calculate a checksum in several ways, some more reliable than others and that reliability usually comes at a price in compexity of the algorithm and its computation time, and also the size of the checksum.
So, what are the circumstances and limiting factors?

Edit: Ugh, too slow.

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.

duhit Choudhary
Ranch Hand

Joined: Apr 01, 2012
Posts: 64

posted Sep 07, 2012 06:42:55

0

thanks for replying i made a code like this but not getting the correct output. tell me where i m wrong...
here is my code.......

Just FYI, neither of the two methods you've chosen are likely to yield very good checksums. The first probably and the second for sure won't highlight transposition errors. I suggest you do a quick Google for checksum algorithms; there are tons around.

Also, if the checksum is only a byte long you have a 1:256 chance of getting a collision by accident. With an int (4 bytes) it would be 1:2^32 and equally fast (in fact, possibly faster).

Winston Gutkowski wrote:Just FYI, neither of the two methods you've chosen are likely to yield very good checksums.

There isn't much point in worrying whether it's a good checksum or not, if what you do next is to modify the array by replacing its last entry, is there? Because now you have a checksum of some list of values which no longer exists.

Paul Clapham wrote:There isn't much point in worrying whether it's a good checksum or not, if what you do next is to modify the array by replacing its last entry, is there? Because now you have a checksum of some list of values which no longer exists.

I presume you're referring to the first post, because I don't see any sign of it in the code.

@duhit: Paul's quite right though. You certainly don't want to overwrite any of the array with your checksum. Normal practise is to add it to the front.

To be less specific, this all smells like a homework problem, so criticizing it for reality-based reasons isn't necessarily all that useful.

duhit Choudhary
Ranch Hand

Joined: Apr 01, 2012
Posts: 64

posted Sep 10, 2012 00:27:19

0

thanks to all of you...
@Paul : it really was a homework problem and I found a solution with a new problem though. I used a byte array in my code which is

now the problem in my code was only that compiler is not taking the same value as given in the array. I wanted to take values like 58, 80, D, 1, 4 and checksum of the same. so now please help me and tell me how can I obtain the above values from the values given in my code in the array.
i.e how can i make my byte array to be appear like this for the compile so it can give the correct output