【问题标题】:iOS:CRC in obj ciOS:obj c 中的 CRC
【发布时间】:2014-12-11 06:20:07
【问题描述】:

我是 iOS 新手,我需要使用 CRC 算法为以下命令创建数据包

 int comm[6];
 comm[0]=0x01;
 comm[1]=6;
 comm[2]=0x70;
 comm[3]=0x00;
 comm[4]=0xFFFF;
 comm[5]=0xFFFF;

我有一个 java 代码,它与在 android 中开发的东西相同

         byte[] getCRC(byte[] bytes)
         {
            byte[] result = new byte[2];
            try
            {
                        short crc = (short) 0xFFFF;
                        for (int j = 0; j < bytes.length; j++)
                        {
                               byte c = bytes[j];
                                for (int i = 7; i >= 0; i--)
                                 {
                                    boolean c15 = ((crc >> 15 & 1) == 1)
                                    boolean bit = ((c >> (7 - i) & 1) == 1);
                                    crc <<= 1;
                                    if (c15 ^ bit)
                                    {
                                 crc ^=  0x1021; // 0001 0000 0010 0001 (0, 5, 12)

       }
                                            }
                            }
                            int crc2 = crc - 0xffff0000;
                            result[0] = (byte) (crc2 % 256);
                            result[1] = (byte) (crc2 / 256);


            return result;
            }
            catch(Exception ex)
            {
                            result = null;
                            return result;
            }

         }

getCRC() 方法的输入:要计算 CRC 的数据包。

getCRC() 方法的输出:数据包的 CRC。

我需要在 obj c 中做同样的事情,如果还有可用的示例代码,请提供帮助。

【问题讨论】:

    标签: objective-c iphone crc32


    【解决方案1】:

    Objective-C 还包含 C,因此您的方法的内容看起来与 Java 中的内容几乎相同。所需要的只是将数据传入和传出方法,在本示例中使用 NSData:

    - (NSData *)bytesCRCResult:(NSData *)dataBytes
    {
        unsigned char *result = (unsigned char *)malloc(2);
        unsigned char *bytes = (unsigned char *)[dataBytes bytes]; // returns readonly pointer to the byte stream
    
        uint16_t crc = (short) 0xFFFF;
        for (int j = 0; j < dataBytes.length; j++)
        {
            unsigned char c = bytes[j];
            for (int i = 7; i >= 0; i--)
            {
                bool c15 = ((crc >> 15 & 1) == 1);
                bool bit = ((c >> (7 - i) & 1) == 1);
                crc <<= 1;
                if (c15 ^ bit)
                {
                    crc ^=  0x1021; // 0001 0000 0010 0001 (0, 5, 12)
                }
            }
        }
    
        uint16_t crc2 = crc - 0xffff0000;
        result[0] = (unsigned char) (crc2 % 256);
        result[1] = (unsigned char) (crc2 / 256);
    
        NSData *resultsToData = [NSData dataWithBytes:result length:2];
        free(result);
        return resultsToData;  
    }
    

    NSData 可以使用 [NSData bytes] 方法调用作为原始字节读取,并且具有一系列有用的属性和方法。

    对于布尔值,您有几个选择:

    • “bool”似乎是 ISO C/C++ 标准类型
    • “布尔”定义为“typedef unsigned char”
    • “boolean_t”被定义为“typedef unsigned int”或“typedef int”,显然取决于64位编译
    • “BOOL”,Objective-C bool,根据http://nshipster.com/bool/ 定义为“typedef signed char”,因此可能无法按预期运行。

    为了清楚起见,“uint8_t”可以替换为“unsigned char”。

    请注意:以上代码编译时没有警告或投诉,但未使用实际数据进行测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-26
      • 1970-01-01
      • 2013-02-16
      • 2011-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多