Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.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# 使用ArraySegment进行校验和计算<;字节>;_C# - Fatal编程技术网

C# 使用ArraySegment进行校验和计算<;字节>;

C# 使用ArraySegment进行校验和计算<;字节>;,c#,C#,我对下面的方法有异议-我不明白它为什么会这样做 private static bool chksumCalc(ref byte[] receive_byte_array) { Console.WriteLine("receive_byte_array -> " + receive_byte_array.Length); //ok,151 bytes in my case ArraySegment<byte> segment = new ArraySegment&

我对下面的方法有异议-我不明白它为什么会这样做

private static bool chksumCalc(ref byte[] receive_byte_array)
{
    Console.WriteLine("receive_byte_array -> " + receive_byte_array.Length); //ok,151 bytes in my case
    ArraySegment<byte> segment = new ArraySegment<byte>(receive_byte_array, 0, 149);
    Console.WriteLine("segment # -> " + segment.Count); //ok,149 bytes

    BitArray resultBits = new BitArray(8); //hold the result
    Console.WriteLine("resultBits.Length -> " + resultBits.Length); //ok, 8bits
    //now loop through the 149 bytes
    for (int i = segment.Offset; i < (segment.Offset + segment.Count); ++i)
    {
        BitArray curBits = new BitArray(segment.Array[i]);
        Console.WriteLine("curBits.Length -> " + curBits.Length); //gives me 229 not 8?
        resultBits = resultBits.Xor(curBits);
    }

    //some more things to do  ... return true...
    //or else
    return false;
}
private static bool chksumCalc(ref byte[]接收字节数组)
{
Console.WriteLine(“receive\u byte\u array->”+receive\u byte\u array.Length);//好的,在我的例子中是151字节
ArraySegment段=新的ArraySegment(接收字节数组,0,149);
Console.WriteLine(“段#->”+段.计数);//好的,149字节
BitArray resultBits=新的BitArray(8);//保留结果
Console.WriteLine(“resultBits.Length->”+resultBits.Length);//好的,8比特
//现在循环149字节
对于(int i=段.偏移;i<(段.偏移+段.计数);+i)
{
BitArray curBits=新的位数组(段数组[i]);
Console.WriteLine(“corbits.Length->”+corbits.Length);//给我229而不是8?
resultBits=resultBits.Xor(curBits);
}
//还有一些事情要做…返回真实。。。
//否则
返回false;
}

我需要
XOR
149字节,我不明白为什么
segment.Array[I]
不给我1字节。如果我使用149字节的数组,例如
segment.array[1]
它必须产生第二个字节,还是我错了?229从哪里来?有人能澄清一下吗?谢谢。

这是您要呼叫的构造函数:

初始化BitArray类的新实例,该实例可以保存指定数量的位值,这些值最初设置为false

如果你看的话,对于
BitArray
,你可以这样读。不过,我不明白为什么需要使用
BitArray
类。只需使用
字节
来存储异或结果:

private static bool chksumCalc(ref byte[] receive_byte_array)
{
    var segment = new ArraySegment<byte>(receive_byte_array, 0, 149);
    byte resultBits = 0;
    for (var i = segment.Offset; i < (segment.Offset + segment.Count); ++i)
    {
        var curBits = segment.Array[i];
        resultBits = (byte)(resultBits ^ curBits);
    }

    //some more things to do  ... return true...
    //or else
    return false;
}
private static bool chksumCalc(ref byte[]接收字节数组)
{
var段=新数组段(接收字节数组,0,149);
字节结果比特=0;
对于(变量i=段.偏移量;i<(段.偏移量+段.计数);+i)
{
var curBits=段.数组[i];
resultBits=(字节)(resultBits^corbits);
}
//还有一些事情要做…返回真实。。。
//否则
返回false;
}

我认为您也不需要
ArraySegment
(不是针对提供的代码),但我保留了它,因为它与问题无关。

这是您正在调用的构造函数:

初始化BitArray类的新实例,该实例可以保存指定数量的位值,这些值最初设置为false

如果你看的话,对于
BitArray
,你可以这样读。不过,我不明白为什么需要使用
BitArray
类。只需使用
字节
来存储异或结果:

private static bool chksumCalc(ref byte[] receive_byte_array)
{
    var segment = new ArraySegment<byte>(receive_byte_array, 0, 149);
    byte resultBits = 0;
    for (var i = segment.Offset; i < (segment.Offset + segment.Count); ++i)
    {
        var curBits = segment.Array[i];
        resultBits = (byte)(resultBits ^ curBits);
    }

    //some more things to do  ... return true...
    //or else
    return false;
}
private static bool chksumCalc(ref byte[]接收字节数组)
{
var段=新数组段(接收字节数组,0,149);
字节结果比特=0;
对于(变量i=段.偏移量;i<(段.偏移量+段.计数);+i)
{
var curBits=段.数组[i];
resultBits=(字节)(resultBits^corbits);
}
//还有一些事情要做…返回真实。。。
//否则
返回false;
}

我认为您也不需要
ArraySegment
(不是针对提供的代码),但我保留原样,因为它与问题无关。

看起来您正在使用此构造函数创建
BitArray
。我想您需要
新的位数组(BitConverter.GetBytes(segment.Array[I])@Johnny Mopp-它现在给出了curBits.Length->16。我仍然不明白为什么它不是8它不是8,因为数组中该位置的值是16,所以构造函数将创建一个
BitArray
,它可以包含那么多的值。看看我的答案。哦,好的。查看“没有接受
字节的重载”
。调用
BitConverter.GetBytes(segment.Array[i])
有点愚蠢,因为您已经从一个字节开始了。它可用于将64位长的值扩展为字节数组,但在此处没有应用程序。看起来您正在使用此构造函数创建
BitArray
。我想您需要
新的位数组(BitConverter.GetBytes(segment.Array[I])@Johnny Mopp-它现在给出了curBits.Length->16。我仍然不明白为什么它不是8它不是8,因为数组中该位置的值是16,所以构造函数将创建一个
BitArray
,它可以包含那么多的值。看看我的答案。哦,好的。查看“没有接受
字节的重载”
。调用
BitConverter.GetBytes(segment.Array[i])
有点愚蠢,因为您已经从一个字节开始了。它可以用来将一个64位长的值扩展为一个字节数组,但它在这里没有应用程序。不,我不需要它-我删除了它。又是Thx。不,我不需要它-我把它拿走了。又是Thx。