Bookmark Topic Watch Topic
  • New Topic

Calculate checksum for a byte array

 
duhit Choudhary
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Report post to moderator
and in how many ways can a checksum can be done?
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Report post to moderator
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: 1680
22
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Your second 'for' loop is incorrect.

What values where you expecting? What values did you get?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
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: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
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: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Pie
Posts: 50168
79
  • Likes 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
Since that last post appears to be a duplicate of this thread, I am taking the liberty of locking this discussion.
 
    Bookmark Topic Watch Topic
  • New Topic