Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#到F#CRC16^和^^^的工作原理不同。^如何工作?_C#_F#_Crc16 - Fatal编程技术网

C#到F#CRC16^和^^^的工作原理不同。^如何工作?

C#到F#CRC16^和^^^的工作原理不同。^如何工作?,c#,f#,crc16,C#,F#,Crc16,我找到了CRC16的C代码,但我需要它在F上: 使用制度 public class Crc16 { const ushort polynomial = 0xA001; ushort[] table = new ushort[256]; public ushort ComputeChecksum(byte[] bytes) { ushort crc = 0; for(int i = 0; i < bytes.Length; ++i)

我找到了CRC16的C代码,但我需要它在F上:

使用制度

public class Crc16 {
    const ushort polynomial = 0xA001;
    ushort[] table = new ushort[256];

    public ushort ComputeChecksum(byte[] bytes) {
        ushort crc = 0;
        for(int i = 0; i < bytes.Length; ++i) {
            byte index = (byte)(crc ^ bytes[i]);
            crc = (ushort)((crc >> 8) ^ table[index]);
        }
        return crc;
    }

    public byte[] ComputeChecksumBytes(byte[] bytes) {
        ushort crc = ComputeChecksum(bytes);
        return BitConverter.GetBytes(crc);
    }

    public Crc16() {
        ushort value;
        ushort temp;
        for(ushort i = 0; i < table.Length; ++i) {
            value = 0;
            temp = i;
            for(byte j = 0; j < 8; ++j) {
                if(((value ^ temp) & 0x0001) != 0) {
                    value = (ushort)((value >> 1) ^ polynomial);
                }else {
                    value >>= 1;
                }
                temp >>= 1;
            }
            table[i] = value;
        }
    }
}

所以我认为C版本在这里取第一或第二个字节。所以我想知道C#'^'在这里是如何工作的?我如何将这行C代码翻译成F?

C
^
和F
^^
都是异或运算符。它们应该是一样的。这就是你要问的吗?

C#
^
和F#
^^
都是异或运算符。它们应该是一样的。这就是你要问的吗?

这计算的结果与你的C代码相同


这将计算与C#代码相同的结果


实际上是强制转换操作符
(byte)
正在“获取”第一个字节。您只需要在F#中执行类似的强制转换:
index=byte(crc^^^^^bytes.[i])
此表达式的类型应为uint16,但这里的F#中的类型为bytenote,字节的文本为
uy
而非
us
-这就是导致XOR问题的原因-并解释了C#示例(ushort crc=0;)中的编译器错误crc是2个字节(16位)(毕竟我想得到的是crc16,不是crc8),而且没有错误,所以^and^^^^的工作原理不同,我想知道^s如何在F#上执行相同的操作。哦,我明白了。也许你需要
index=(byte crc)^^^^^字节。[i]
。实际上是强制转换操作符
(byte)
在“获取”第一个字节。您只需要在F#中执行类似的强制转换:
index=byte(crc^^^^^bytes.[i])
此表达式的类型应为uint16,但这里的F#中的类型为bytenote,字节的文本为
uy
而非
us
-这就是导致XOR问题的原因-并解释了C#示例(ushort crc=0;)中的编译器错误crc是2个字节(16位)(毕竟我想得到的是crc16,不是crc8),而且没有错误,所以^and^^^^的工作原理不同,我想知道^s如何在F#上执行相同的操作。哦,我明白了。也许您需要
索引=(字节crc)^^^^字节。[i]
let ComputeChecksum(bytes : byte array) =
    let mutable crc = 0us
    for i = 0 to bytes.Length do
        let index = (crc ^^^ bytes.[i]) // ? uint16 and byte
type Crc16() =
  let polynomial = 0xA001us
  let table = Array.init 256 (fun i ->
    ((0us, uint16 i), [0y..7y]) 
    ||> Seq.fold (fun (value, temp) j ->
      let newValue = 
        match (value ^^^ temp) &&& 0x0001us with
        | 0us -> value >>> 1
        | _ -> ((value >>> 1) ^^^ polynomial)
      newValue, temp >>> 1)
    |> fst)
  member __.ComputeChecksum(bytes:byte[]) =
    (0us, bytes) ||> Seq.fold (fun crc byt ->
      let index = byte (crc ^^^ (uint16 byt))
      (crc >>> 8) ^^^ table.[int index])