简体   繁体   中英

Android : Custom Checksum Calculation

I want to calculate custom checksum calculation in java.help me out . how to do the below example operation ?

example :

data : 30313037303130453030

hex summation :
x = ' 30+31+30+37+30+31+30+45+30+30' = '1FE'

Not of hex value :
y = NOT(x) = 01

Answer : Checksum = 3031

I have found a nice tut about the cksum calculation (it's in java) - here . Here's also a .jar that can be helpful - here . There's even a class Checksum, which can be found here - LINK .

To obtain the "cksum" that you're talking about (because it is really a fake "cksum" that you're creating and can really cause you some problems if you rely on the uniqueness of it among the same strings representing different files etc.) you can use this:

int value = 0;
String incHex = "";

for (int i = 0; i == data.length(); i+=2){
  valueInt += Integer.parseInt(hex, data.substring(i, i+2));
}
resultHex = Integer.toHexString(valueInt);

This resultHex is the "x" from your example, the valueInt is the decimal representation of that numer (the sum of all the parts of the data string).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM