English 中文(简体)
Equivalent to Unix cksum inWindows
原标题:Equivalent to Unix cksum in Windows

我下载了档案和检查(由cks星“Unix”指挥部制作)。

因此,我想,在我的“C#”测试中,如果支票与我下载的那部分支票相隔。

我在Couix人页上检查:

  The cksum command calculates and prints to standard output a checksum
  for each named file, the number of octets in the file and the
  filename.

  cksum uses a portable algorithm based on a 32-bit Cyclic Redundancy
  Check.  This algorithm finds a broader spectrum of errors than the
  16-bit algorithms used by sum (see sum(1)).  The CRC is the sum of the
  following expressions, where x is each byte of the file.

       x^32 + x^26 + x^23 +x^22 + x^16 + x^12 + x^11 + x^10 + x^8 + x^7
       + x^5 + x^4 + x^2 + x^1 + x^0

  The results of the calculation are truncated to a 32-bit value.  The
  number of bytes in the file is also printed.

因此,我撰写了一个简单的方案,其数额是:

byte[] arr = File.ReadAllBytes(@"MyApp").ToArray();

int cksum = 0;

foreach (byte x in arr)
{
    cksum += (x ^ 32 + x ^ 26 + x ^ 23 + x ^ 22 + x ^ 16 + x ^ 12 + x ^ 11 + x ^ 10 + x ^ 8 + x ^ 7 + x ^ 5 + x ^ 4 + x ^ 2 + x ^ 1 + x ^ 0);
}

但是,检查也一样,我如何确定这一点?

增 编


EDIT

(1) 修改后的算法是:

uint cksum = 0;

foreach (byte b in arr)
{
    var x = (uint)b;

    cksum += (IntPow(x, 32)
        + IntPow(x, 26) + IntPow(x, 23) + IntPow(x, 22)
        + IntPow(x, 16) + IntPow(x, 12) + IntPow(x, 11) + IntPow(x, 10)
        + IntPow(x, 8) + IntPow(x, 7) + IntPow(x, 5) + IntPow(x, 4) + IntPow(x, 2) + IntPow(x, 1) + IntPow(x, 0));
}

2) I used the class Crc32 : HashAlgorithm

鉴于Crc32号的“Unix”档案:2774111254

  • 1) Gives me : 4243613712
  • 2) Gives me : 3143134679 (with a seed of 0)

我做什么错了!

最佳回答
问题回答

Those are powers, not xors. See Wikipedia about CRC.

c c# ^不是提高能力操作者,而是Xor操作者,而《儿童权利公约》则使用并非特定语言的一般数学术语撰写。

Don t use a standard "pow" function either since those will normally use floating point to represent very large numbers such as x^32.

你们想要的是保留回答的“low>/em> 32 bits”。 或许最好的办法是:

  • Write your own integer-power function, which takes arguments as Ints and calculates x^N for you by doing multiplication of x N times, and trusting the .NET runtime to be reasonably efficient (or if it s too slow, using some optimisation like doing exponentiation by squaring). Don t let the numbers grow too big, either by rounding off after each multiple using the remainder or bitwise-and operators, or using unchecked values and trusting them to wrap round and keep the low 32 bits each time.
  • Look for a library or existing code which calculates a CRC32 directly (eg. http://www.google.co.uk/search?q=c%23+crc32)

Also see this correct implementation https://cksum.codeplex.com/





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...