BCH -CRC在C中提供帮助

时间:2014-12-20 05:46:18

标签: c binary crc

我想让this在C中工作C中的CRC计算似乎很容易但是我的CRC / BCH例程没有产生与第一个代码字的链接相同的答案,即使我尝试手动计算甚至与链接中给出的内容不匹配。

我的手动计算是

           11101101001    101010011010000010100
                          111011010010000000000
                          010001001000000010100
                           11101101001
                           01100100001000010100
                            11101101001
                            0010010101100010100
                              11101101001
                              01111000101010100
                               11101101001
                               0001100001110100
                                  11101101001
                                  0010111010000
                                    11101101001
                                    01010111001 ---- remainder (should match with BCH(31,21) value in the link)

我不确定我错在哪里。这是我的代码

int calc_bch_and_parity( int cw_e ) 
{ 
    int bit=0; 
    int local_cw = 0;  
    int parity = 0; 

    local_cw = cw_e;  
    for( bit=1; bit<=21; bit++, cw_e <<= 1 ) 
        if (cw_e & 0x80000000) 
            cw_e ^= 0xED200000; 

    local_cw |= (cw_e >> 21); 
    cw_e =local_cw; 
    for( bit=1; bit<=32; bit++, cw_e <<= 1 ) 
        if (cw_e & 0x80000000) 
            parity++; 

   return (parity%2) ? local_cw+1 : local_cw; 
} 

******************************编辑的代码,以便更清楚地了解流程******** ******

 int bit=0;
  int local_cw = 0;
  int parity = 0;
 // int try;
  int  answer;
 // int genpoly = 0x1DA400;
  local_cw=cw;  /* bch */

  for(bit=1; bit<=21; bit++, cw <<= 1)
    {
    if (cw & 0x80000000) 
    {   
    cw ^= 0xED200000;
    printf ("mod2 remainder is %x\n", cw);

    }

    }
    printf ("original data is %x\n", local_cw);
  local_cw |= (cw >> 21);
    printf ("21 bit right shifted remainder is %x\n",cw>>21);

  cw =local_cw;  /* parity */
  for(bit=1; bit<=32; bit++, cw<<=1)
    if (cw & 0x80000000) parity++;
{

   if(parity%2) 
    {
     printf("codeword is %x \n",local_cw+1 );
    answer = local_cw+1;
    }
   else
    {
     printf("codeword is %x \n",local_cw );
     answer =local_cw;
    }   
}

    return answer;

预期提醒是1001111100

BCH的另一个例子。

我尝试过的另一件事是http://www.codeproject.com/Articles/13189/POCSAG-Encoder,抱歉我必须依赖外部链接,因为我无法发布图片,在图片的这个链接中你会看到有一个预编码消息以十六进制显示,实际消息是单词“Salam,我设法使用此预编码消息成功地将消息传送到寻呼机(将其作为十六进制整数数组送到发送器),但即使是此消息,即。单词“Salam”在编码消息中不可见(至少是第一个或最后一个或任何20个连续位)。我将二进制中的整个十六进制消息和二进制中的单词“Salam”转换为第一个或最后一个20位编码消息中的“Salam”一词。现在对于CRC(基本上是BCH)来说这是非常奇怪的,CRC不会假设编码/扭曲实际消息,它应该只用余数和奇偶校验附加它(以防万一)因此,实际的消息应该是可见的,第一个或最后的20位,我甚至尝试改变endianess但没有喜悦。这是det我对这个新例子努力的努力

Salam:01010011 01100001 01101100 01100001 01101101

gx:10010110111

Salam_reversed_by_byte:11001010 10000110 00110110 10000110 10110110

Salam_ rev:10110110 10000110 00110110 10000110 11001010

1234567:00110001001100100011001100110100001101010011011000110111

1234567_reverse:11101100011011001010110000101100110011000100110010001100

萨拉姆的前20位:01010011011000010110萨拉姆的First20_reverse:01101000011011001010

Hex CW:0x4B5A1A25,0xE5866E6E,0x7CD215D8,0xE1DB221B,0x84081630

0x4B5A1A25:01001011010110100001101000100101

0xE5866E6E:11100101100001100110111001101110

0xE1DB221B:11100001110110110010001000011011

0x84081630:10000100000010000001011000110000

1 个答案:

答案 0 :(得分:1)

对我而言,我所做的只是将红利填充到32位

10101001101000001010000000000000
11101101001
01000100100000001010000000000000
 11101101001
00110010000100001010000000000000
  11101101001
00001001010110001010000000000000
    11101101001
00000111100010101010000000000000
     11101101001
00000000111000111010000000000000
        11101101001
00000000000011101000000000000000
            11101101001
00000000000000000101001000000000
                 11101101001
00000000000000000010010010010000
                  11101101001
00000000000000000001111111011000
                   11101101001
00000000000000000000001001111100

这是执行计算的代码&#34;手工&#34;

#include <stdio.h>
#include <string.h>

char blank[]    = "                                ";
char dividend[] = "10101001101000001010000000000000";
char divisor[]  = "11101101001";

int main( void )
{
    int i, j;

    int N = strlen( dividend );
    int M = strlen( divisor );
    printf( "%d %d\n", N, M );

    for ( i = 0; i < N - M; i++ )
    {
        if ( dividend[i] == '1' )
        {
            printf( "%s\n", dividend );
            printf( "%s%s\n", &blank[N-i], divisor );
            for ( j = 0; j < M; j++ )
            {
                if ( dividend[i+j] == divisor[j] )
                    dividend[i+j] = '0';
                else
                    dividend[i+j] = '1';
            }
        }
    }
    printf( "%s\n", dividend );
}

这里有同样的东西......

#include <stdio.h>
#include <stdint.h>

int main( void )
{
    uint32_t dividend  = 0x153414;
    uint32_t generator = 0x769;
    uint32_t mask;

    dividend  <<= 11;                   // pad the dividend with 11 zeros
    generator <<= 21;                   // left justify the generator
    mask = 1 << 31;                     // start the mask at the MSB
    for ( int i = 0; i < 21; i++ )
    {
        if ( dividend & mask )          // if the dividend has 1 at the current mask position
            dividend ^= generator;      // "subtract" the generator
        generator >>= 1;                
        mask >>= 1;                     // move to the next bit
    }

    printf( "%08x\n", dividend );       // whatever's left is the remainder
}