问题
A sample of the data is:
de 55 7a ff 41 4e 3b
.. .. .. .. .. .. .. .. .. .. .
.
the sum of the first six numbers (in hex) is 33b
and the "checksum" number is 3b
, as the last 2 characters of the sum.
What's the name of this check sum?
How can i make it in Objective C?
Thank you
回答1:
To get checksum of a hex string is really straight forward, most of the time, we are just overthinking it.
If you have a NSMutableData and want to get checksum of it, please check my answer here.
If you have a Byte Array:
For example:
Byte receivedHex[7] = {0xde, 0x55, 0x7a, 0xff, 0x41, 0x4e, 0x3b};
It's even simpler:
Byte checksum;
for (int i = 0; i < 7; i++){
checksum += receivedHex[i];
checksum = checksum&0xff;
}
回答2:
I found the solution
Firstly, i must convert hex to NSData. After that, convert NSData to NSMutableArray. With NSMutableArray i can calculate the sum of six number.
来源:https://stackoverflow.com/questions/19772650/checksum-of-a-hex-string-in-objective-c