Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Calculate checksum for a byte array

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
and in how many ways can a checksum can be done?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.......
 
Saloon Keeper
Posts: 9717
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Your second 'for' loop is incorrect.

What values where you expecting? What values did you get?
 
lowercase baba
Posts: 13081
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 27450
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 27450
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 76825
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic