Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3458070
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:56:15+00:00 2026-05-18T09:56:15+00:00

believe me I have tried to code this, tried Google, and haven’t had any

  • 0

believe me I have tried to code this, tried Google, and haven’t had any luck.
I’m trying to implement a CRC16 using this poly

x^16 + x^10 + x^8 + x^7 + x^3 + 1

using the C language. Since I understand PHP better I’m trying to get a function going, but I’m not getting the right answer of 28713. This code is generating a CRC of 32713.

function crc16($string,$crc=0) {

for ( $x=0; $x<strlen( $string ); $x++ ) {

    $crc = $crc ^ ord( $string[$x] );
    echo $crc.'<br />';
    for ($y = 0; $y < 8 ; $y++) {

        if ( ($crc & 0x0001) == 0x0001 ) $crc = ( ($crc >> 1 ) ^ 0x10589  );
        else                             $crc =    $crc >> 1;
    }
}

    return $crc;
}


echo 'CRC:'.crc16('10100011');

Please I beg anyone to give a hand with this..thanks in advance.

  • 1 1 Answer
  • 4 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:56:16+00:00Added an answer on May 18, 2026 at 9:56 am
    1. Some CRCs are defined to process the bits from each byte from MSB to LSB, and some are defined to process bits from LSB to MSB (the latter is generally the order which is described as “reflected” and uses a reversed polynomial). Your code puts new bits in at the LSB end of the CRC and shifts right, which is suitable for a reflected CRC, but CRC-16-DECT appears to be one of the non-reflected ones.

    2. Your input of “10100011” suggests binary, but is being processed as an 8-byte ASCII string.

    To see what happens when treating 10100011 as binary instead, and working from MSB first, here’s a hand calculation (as 8 bits of input doesn’t require very much effort):

    polynomial coefficients
            |
            |            10100010  <--- quotient (irrelevant)
            v          __________
     10000010110001001 ) 10100011  <-------- input
                       ^ 10000010110001001
                         -----------------
                       =   100001110001001
                         ^ 10000010110001001
                           -----------------
                         =      101110101101
                              ^ 10000010110001001
                                -----------------
       remainder (CRC) -----> =   111000000101001
         = 0x7029 = 28713
    

    So treating the input as binary and working MSB first is the right thing to do.

    Here is some C code to do the job (as I’m not really into PHP, and ultimately you want C code anyway):

    #include <stdio.h>
    #include <stdint.h>
    
    static uint16_t crc16(const uint8_t *data, size_t len)
    {
        size_t i, j;
        uint16_t crc = 0;
    
        for (i = 0; i < len; i++) {
            crc ^= (data[i] << 8);              /* data at top end, not bottom */
            for (j = 0; j < 8; j++) {
                if ((crc & 0x8000) == 0x8000)   /* top bit, not bottom */
                    crc = (crc << 1) ^ 0x0589;  /* shift left, not right */
                else
                    crc <<= 1;                  /* shift left, not right */
            }
        }
    
        return crc;
    }
    
    int main(void)
    {
        const uint8_t in[] = { 0xa3 };          /* = 10100011 in binary */
        uint16_t crc = crc16(in, sizeof(in));
    
        printf("%u (0x%x)\n", crc, crc);
        return 0;
    }
    

    Result:

    $ gcc -Wall -o crc16 crc16.c
    $ ./crc16  
    28713 (0x7029)
    $ 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two different-sized monitors, connected together using (I believe) TwinView. I tried System.out.println(Toolkit.getDefaultToolkit().getScreenSize());
This is staight forward I believe: I have a table with 30,000 rows. When
I've recently been trying to port a C++ application. I believe I have all
I believe this question might have been previously attempted in 2006 on a different
I do have Google Analytics on my site (tied to adwords), and have tried
I have tried many ways of Asynchronously loading images and they ALL have this
I have the following query in ASP.NET/C# code which is failing to return any
I'm trying to get the apns daemon at http://code.google.com/p/apnsd/ working, and am having problems
I'm trying to include the Google Website Optimizer JavaScript code, below, in a Zope3
Note: I have seen this and tried to take as much from it as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.