wood burning stoves 2.0*
The moose likes Java in General and the fly likes Calculate checksum for a byte array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

Calculate checksum for a byte array

duhit Choudhary
Ranch Hand

Joined: Apr 01, 2012
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

Joined: Apr 01, 2012
Posts: 64
and in how many ways can a checksum can be done?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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

Joined: Mar 10, 2008
Posts: 1768
    
    7

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
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
Ranch Hand

Joined: Nov 19, 2001
Posts: 180

Your second 'for' loop is incorrect.

What values where you expecting? What values did you get?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

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
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

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

Joined: Mar 17, 2011
Posts: 8008
    
  22

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
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

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

Joined: Oct 13, 2005
Posts: 39393
    
  28
Since that last post appears to be a duplicate of this thread, I am taking the liberty of locking this discussion.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calculate checksum for a byte array