English 中文(简体)
《儿童权利公约》第3版的我的贾瓦港有什么错误?
原标题:What is wrong with my Java port of this C++ CRC32 function?
  • 时间:2012-05-14 20:55:41
  •  标签:
  • java
  • c++
  • crc

我的《儿童权利公约》职能如下:

unsigned long Checksummer::crc32 (unsigned long crc, char *buf, unsigned long len)
{
   unsigned long crc_table[256];
   int i, k;
   for (i = 0; i < 256; i++) {
      unsigned long c = (unsigned long) i;
      for (k = 0; k < 8; k++) 
         c = c & 1 ? 0xedb88320 ^ (c >> 1) : c >> 1;
      crc_table[i] = c;
   }

   crc = crc ^ 0xffffffffL;
   while (len--) 
      crc = crc_table[((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8);
   return crc ^ 0xffffffffL;
}

我正试图将这一法典送到 Java,以便我能够在多个平台上计算《儿童权利公约》。 下面的我携带的法典产生了不同的结果。 我做了什么错误?

static long getCrc32 (long crc, char[] buf, long len)
{
   long crc_table[] = new long[256];
   int i, k;
   for (i = 0; i < 256; i++) {
      long c = ( long)i;
      for (k = 0; k < 8; k++) 
         c = (c & 1) == 1 ? 0xedb88320 ^ (c >> 1) : c >> 1;
      crc_table[i] = c;
    }

    /* Calculate crc on buf */
    crc = crc ^ 0xffffffffL;
    int j = 0;
    while (len-- != 0){ 
       crc = crc_table[((int)crc ^ (buf[(int)j++])) & 0xff] ^ (crc >> 8);}
    return crc ^ 0xffffffffL;
}
最佳回答

<代码>char为 Java的两条 by;byte改为(char in C and C++ is one byte by definition).

<代码>long为 Java的八英寸;int,改为(假设sizeof (unsign long) ==4,载于你的C++平台,这是规范。

问题回答

我看到的一个问题是:

c = (c & 1) == 1 ? 0xedb88320 ^ (c >> 1) : c >> 1;

C++,c&1 确实,它不是零;它不一定是1。

此外,您或许应当使用<代码>>>>而不是>>。 前者总是将0条移至左边轨道,就像C营运人>>不签字。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签