September 6th, 2011, 05:36 AM
-
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.
September 6th, 2011, 06:18 AM
-
You need to do some fancy casting, or create a union and access the parts that way. Beyond curiosity, why are you not just getting some source off the 'net? CRC calculations are nearly as old as computers and I am sure you can get any sized version for any architecture (8, 16, 32 or 64 bit) in a few seconds of searching.
September 6th, 2011, 06:40 AM
-
Aparently the CRC24 is not that popular, but I found some implementations on the internet only I can't get them to work well for me. I don't know where the problem is.
What kind of casting? Can you be a little more specific please?
September 6th, 2011, 07:20 AM
-
Check out http://www.tty1.net/pycrc/
You need to get a pointer to the start of your 16 bit chunk of memory, then reassure the compiler you know what you are doing by casting the pointer to an unsigned short (be sure that means 16 bits in your architecture!). It is some pretty ugly code and I am reluctant to post it because without really understanding what is going on it is nearly impossible to debug. Since you are also only looking at 24 bits (and 16 does not evenly divide into 24), you also have some bit shifting and masking to do, all of which is very easy to get wrong. You should download the code at the link I provided and look over it for some ideas (or just use it). Though it is probably very dense with typedefs, bit fiddling and whatnot in order to maximize performance and reuse code, that is the sort of code you are going to have to get familiar with if you want to work at this level.
September 6th, 2011, 09:53 AM
-
While I know you're new with this site, it should be mentioned that Cross-posting is frowned upon as it can cause a duplication of effort on the part of the two communities. It's generally considered best not to cross-post at all, but if you feel you would get better results this way (and not annoy people to the point that they ignore you), please at least mention that you've posted the same question elsewhere so the groups can review what was said elsewhere.
Fortunately, I was here to do this for you. :p
EDIT: Seems you've been busy with the cut and paste:
C Board
Stack Overflow
Webmaster Talk
Comments on this post
Last edited by Schol-R-LEA; September 6th, 2011 at 10:00 AM.
September 6th, 2011, 10:10 AM
-
Sorry, I didn't know i have to mention that.
I posted on several forums in order to increase my chances in getting help.
Sorry if I annoyed someone.