0

Hello I have the below c function which I believe to be in the correct specification for CRC-16 CCITT False:

uint16_t crc16(const char* pData, int length)
{
    uint8_t i;
    uint16_t wCrc = 0xffff;
    while (length--) {
        wCrc ^= *(unsigned char *)pData++ << 8;
        for (i=0; i < 8; i++)
            wCrc = wCrc & 0x8000 ? (wCrc << 1) ^ 0x1021 : wCrc << 1;
    }
    return wCrc & 0xffff;
}

I am using Objective C for my application and have the following:

- (IBAction)mycrc16function:(id)sender
{
NSString * stringexample = @"Example";

const char * stringAsChar = [stringexample cStringUsingEncoding:[NSString defaultCStringEncoding]]; //encode string into c string encoding

unsigned long CrcTest = crc16(stringAsChar,sizeof(stringAsChar)); // put encoded string date and size of string into the crc16 function.

NSLog(@"crctext %04lX\n",CrcTest); // print outcome to log
    
}

The output is always incorrect, the string I am testing with for example is giving a result of 6B20

However the result should be E272

I am using this site which I know to be correct to compare my results: http://www.sunshine2k.de/coding/javascript/crc/crc_js.html

Any idea where I am going wrong?

13
  • 3
    I don't know Objective C, but this looks wrong: const char * stringAsChar used as argument for a function that expects a unsigned long. Also sizeof(stringAsChar) is rather strange as it does not depend on the length of that string. Commented Sep 2, 2024 at 14:41
  • 1
    Yes you are correct it was meant to have pointers I was playing around with it and I must have forgot to put it back to its original state. I have simplified the CRC16-CCITT FALSE function with one I have found online instead (now replaced in my question), which seems a bit better put together than my attempt! It's the Obj-C function which feeds the data and outputs the result (NSLog). Still getting a wrong result with the new crc16 function B415 instead of E272 now. Commented Sep 2, 2024 at 15:08
  • 1
    I've already deleted the comment because it was a duplicate of @Gerhardh's comment, but: no. Objective-C is said to be superset to C so sizeof() should be as wrong here as it is in puce C. Just use strlen() if you have a nul terminated string. Btw. my main() is basically uint16_t wCrc = crc16(argv[1], strlen(argv[1]); Commented Sep 2, 2024 at 15:34
  • 1
    This is correct sizeof was returning 8, I hadn't realised that was how sizeof worked, I have changed it to the string length and it's all working now, thanks for your help! Commented Sep 2, 2024 at 15:39
  • 1
    You have made the whole question useless by removing the erroneous code and replacing it by the workging code. What help could any visitor gain from your question now? If you find the solution to your problem, do not vandalize your post but instead post it as an answer to your own question. Commented Sep 2, 2024 at 17:30

1 Answer 1

0

The test string "Example" actually gives 0xE222 in your code, whereas the online calculator gives 0x64CE.

In your code, sizeof(stringAsChar) also accounts for the (invisible) null character, which terminates every string. I assume that you are not including this null character in the online CRC calculator.

To correct this, simply decrease the character count by 1:

unsigned long CrcTest = crc16(stringAsChar,sizeof(stringAsChar)-1); // put encoded string date and size of string into the crc16 function.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.