比较两个字节数组,找出哪个更大

时间:2013-10-08 14:04:30

标签: c# arrays jagged-arrays

我有一个锯齿状的字节数组byte[][],我正在尝试获取它的子数组项的最大值。

我创建了一个方法:

private byte[] Max(byte[][] arrBytes)
{
    var max = arrBytes[0];
    foreach (var arr in arrBytes)
    {
        if (max != null && arr != null)
            if () // => How to say max > arr
                max = arr;
    }
    return max;
}

如何从上述方法返回最大字节数组?

修改 对于所有询问单词bigger的度量或定义的人,意味着我说锯齿状数组包含SQLServer(varbinary(8))的数据类型(时间戳),数据看起来像这样

enter image description here

字节数组表示(例如:0x00000000013F3F3F)

2 个答案:

答案 0 :(得分:2)

也许转换为long并进行比较对您有帮助吗?

// Note: you should ensure that the arrays have at least 8 bytes!
// Although from your edits, it sounds like your "jagged" array isn't jagged at all
if (BitConverter.ToUInt64(max,0) > BitConverter.ToUInt64(arr,0)) 
{
    // do whatever.
}

但要注意字节顺序差异。如果你的时间戳只是一些滴答,这将有效。如果它实际上是一个日期,您需要找出适当的转换。

答案 1 :(得分:0)

你正在寻找这样的东西吗?

    private byte[] Max(byte[][] arrBytes)
    {
        byte[] max = new byte[arrBytes.GetLength(0)];
        int i = 0;
        foreach (byte[] arr in arrBytes)
        {
            byte m = 0;
            foreach (byte b in arr)
            {
                m = Math.Max(m, b);
            }
            max[i] = m; ++i;
        }
        return max;
    }