Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## CRC24Q implementation

I am trying to implement the algorithm of a CRC check, which basically created a value, based on an input message.
So, consider I have a hex message 3F214365876616AB15387D5D59, and I want to obtain the CRC24Q value of the message.
The algorithm that I found to do this is the following:
Code:
```    typedef     unsigned long crc24;
crc24 crc_check(unsigned char *input) {
unsigned char *octets;
crc24 crc = 0xb704ce; // CRC24_INIT;
int i;
int len = strlen(input);
octets = input;

while (len--) {
crc ^= ((*octets++) << 16);

for (i = 0; i < 8; i++) {
crc <<= 1;
if (crc & 0x1000000)
crc ^= CRC24_POLY;
}
}
return crc & 0xFFFFFF;
}```
where *input=3F214365876616AB15387D5D59.
The problem is that ((*octets++) << 16) will shift by 16 bits the ascii value of the hex character and not the character itself.
So, I made a function to convert the hex numbers to characters.
I know the implementation looks weird, and I wouldn't be surprised if it were wrong.
This is the convert function:
Code:
```    char* convert(unsigned char* message) {
unsigned char* input;
input = message;
int p;

char *xxxx[20];
xxxx[0]="";

for (p = 0; p < length(message) - 1; p = p + 2) {
char* pp[20];
pp[0] = input[0];
char *c[20];
*input++;
c[0]= input[0];
*input++;
strcat(pp,c);
char cc;
char tt[2];
cc = (char ) strtol(pp, &pp, 16);
tt[0]=cc;
strcat(xxxx,tt);

}
return xxxx;
}```
SO:
Code:
```    unsigned char *msg_hex="3F214365876616AB15387D5D59";
crc_sum = crc_check(convert((msg_hex)));
printf("CRC-sum: %x\n", crc_sum);```
Thank you very much for any suggestions.

• Yeah your convert function has several bugs.

Code:
`char *xxxx[20];`
This isn't doing what you probably think it does. What you wrote means an array of char* pointers which you never seem to allocate memory for. I'm surprised the program doesn't seg fault.

Do you want your convert function to convert a string of characters to their actual numerical value? e.g. instead of having '1' be be 49 you want it to be 1?

If so then you could do it like so:

Code:
```int convert(unsigned char* input, unsigned char* output)
{
unsigned int index = 0;
int has_errors = 0;

while(input[index] != 0)
{
if(input[index] >= '0' && input[index] <= '9')
output[index] = input[index] - 48;
else if(input[index] >= 'a' && input[index] <= 'f')
output[index] = input[index] - 87;
else if(input[index] >= 'A' && input[index] <= 'F')
output[index] = input[index] - 55;
else
{
//error case
output[index] = 0;
has_errors++
}
}

output[index] = 0;
return has_errors;
}

unsigned char *msg_hex="3F214365876616AB15387D5D59";
unsigned char* converted_msg = (unsigned char*)malloc(strlen(msg_hex) + 1);
if(convert(msg_hex, converted_msg) == 0)
{
crc_sum = crc_check(converted_msg);
}

free(converted_msg);```
I didn't try compiling the code so it might not work 100% but it should be pretty close.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•