checksum of a hex string in objective c

偶尔善良 提交于 2019-12-24 23:41:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!