当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - Calculate CRC 8 in objective c

问题描述:

I've an app in which I need to send a packet to an external device. This packet has a CRC before the end message. The CRC has to be separated in CRCH and CRCL.

For example: CRC = 0x5B so CRCH should be 0x35 (ASCII representation of 5) and CRCL should be 0x42 (ASCII representation of B).

I searched on internet and I found several functions in C or in other language to create CRC32, but my device need to use a CRC8. How I can create a CRC8 in Objective-C? Can you help me to find a way to do this?

网友答案:

Surprising how this rather simple question is still not answered.

First, you need to separate problems in your question. CRH and CRL are just hex conversion and that's easy to do (and has lots of examples on internet too). In most cases, you just need to compare crc you received to one you calculated. So, you just need to convert them to the same form. E.g. convert the crc you calculated to text using sprintf and %2X format and compare with CRC you received (as text).

The second part is actually CRC. This is a little bit trickier.Your options are as follows:

1) the easiest is to rename your .m file to .mm and use CRC library from boost C++. It's just a header include, so it won't affect rest of your code in any way and you can even make it in a separate file, so you'll have a C function which will use boost under the hood. You might need to find parameters for your CRC though. For that, see this excellent resource http://reveng.sourceforge.net/crc-catalogue/

2) You can write your own implementation. Surprisingly there is plenty of examples for particular algorithms in the internet, but they often optimized for particular crc and are hard to adopt for other algorithms.

So, your best bet is probably starting with "A Painless Guide to CRC Error Detection Algorithms" article by Ross Williams. It also includes examples in C. Though it could be complicated to get your head around all the technical stuff and explanations there.

So, as as short cut I'd like to suggest to look at my own implementation in java here. It's obviously not Objective-C. But I looked through it and you should be able to just copy and paste to your .m file and just compile it. Possibly adjusting few types.

You'll need public static long calculateCRC(Parameters crcParams, byte[] data) and private static long reflect(long in, int count) functions there. And the Parameters class which looks scarier, but should just become a struct in your case:

struct Parameters
{
    int width;   // Width of the CRC expressed in bits
    long polynomial; // Polynomial used in this CRC calculation
    bool reflectIn;   // Refin indicates whether input bytes should be reflected
    bool reflectOut;   // Refout indicates whether input bytes should be reflected
    long init; // Init is initial value for CRC calculation
    long finalXor; // Xor is a value for final xor to be applied before returning result
}

You might also want to also adjust types there to a shorter unsigned type (java has no unsigned). But it should work perfectly well as is.

分享给朋友:
您可能感兴趣的文章:
随机阅读: