Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Calculate checksum for a byte array

duhit Choudhary
Ranch Hand
Posts: 64
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
Ranch Hand
Posts: 64
and in how many ways can a checksum can be done?

Winston Gutkowski
Bartender
Posts: 10527
64
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

Winston Gutkowski
Bartender
Posts: 10527
64
• 1
duhit Choudhary wrote:and in how many ways can a checksum can be done?

As many ways as there are leaves in the trees or drops in the ocean. How good they are is another matter.

Winston

Jelle Klap
Bartender
Posts: 1952
7
• 1
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.

duhit Choudhary
Ranch Hand
Posts: 64
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.......

Carey Brown
Bartender
Posts: 1752
22
Your second 'for' loop is incorrect.

What values where you expecting? What values did you get?

fred rosenberger
lowercase baba
Bartender
Posts: 12234
36
duhit Choudhary wrote:thanks for replying i made a code like this but not getting the correct output. tell me where i m wrong...

How are we supposed to help you if you don't tell us a) what you got, and b) what you expect?

Winston Gutkowski
Bartender
Posts: 10527
64
duhit Choudhary wrote: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

Paul Clapham
Sheriff
Posts: 21443
33
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.

Winston Gutkowski
Bartender
Posts: 10527
64
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.

Winston

Paul Clapham
Sheriff
Posts: 21443
33
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
Posts: 64
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

Campbell Ritchie
Sheriff
Posts: 50702
83
• 1
Since that last post appears to be a duplicate of this thread, I am taking the liberty of locking this discussion.