#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    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.
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    248
    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.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0
    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?
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    248
    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.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  8. #5
  9. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,809
    Rep Power
    1574
    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

    • salem agrees : Well said, and thanks for the detective work on the rampant xposting
    Last edited by Schol-R-LEA; September 6th, 2011 at 10:00 AM.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0
    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.

IMN logo majestic logo threadwatch logo seochat tools logo