ASP.NET添加两个字节数组

时间:2013-10-22 17:14:40

标签: asp.net bytearray

我正在看一个练习二加两个字节数组

public AddByteResult ValuesAreAdded(byte[] a, byte[] b)
{
   var result = AddBytes(a, b);
   return new AddResult(a, b, result);
}

以样本数据和结果

给出
Input : { 1, 1, 1 }, { 1, 1, 1 }

Result: {2,2,2}

Input : { 1, 1, 255 }, {0, 0, 1 }

Result: {1,2,0}

所以很明显我需要处理添加字节的函数,但是我遇到的问题是我不理解上面输入的添加。有人可以解释如何计算上述结果以及.NET提供什么来计算字节数组的总和?

3 个答案:

答案 0 :(得分:1)

这是一个朴素的代码示例,用于解释我在评论中提出的建议(我确信有更好的编码逻辑的方法,但希望它能得到满足的要点)......

    static public byte[] AddBytes(byte[] a, byte[] b)
    {
        if (a.Length != b.Length)
        {
            throw new InvalidOperationException("Mismatched array lengths is not currently supported");
        }

        byte[] result = new byte[a.Length + 1];
        int carry = 0;

        for (int x = a.Length - 1; x >= 0; x--)
        {
            int tempresult = a[x] + b[x] + carry;
            result[x + 1] =(byte)(tempresult); 
            carry = tempresult / (byte.MaxValue + 1); 
        }

        if (carry > 0)
        {
            // Carried into extra byte, so return it
            result[0] = (byte)carry;
            return result;
        }
        // no carry into extra byte, so remove it
        return result.Skip(1).ToArray();
    }
    static void Main(string[] args)
    {
        byte[] a = { 1, 1, 1 };
        byte[] b = { 1, 1, 1 };
        byte[] c = { 1, 1, 255 };
        byte[] d = { 0, 0, 1 };
        byte[] e = { 255, 255, 255 };
        byte[] f = { 255, 255, 255 };

        var x = AddBytes(a, b);
        x = AddBytes(c, d);
        x = AddBytes(e, f);
    }

正如我所说,这基本上假设字节数组代表数字......

因此,{1,1,1}相当于0x10101或65793 65793 + 65793 = 131586或0x20202,即{2,2,2}

和,{1,1,255} + {0,0,1}相当于0x101FF + 0x1或66047 + 1 = 66048或0x10200,即{1,2,0}

答案 1 :(得分:0)

你想要一个递归添加2字节数组的函数。看看下面的例子,它可以做你想要的:

private byte[] AddRecursive(byte[] f, byte[] s)
{
    int sum;
    byte[] r;
    int arrayLength = f.Length;

    if (f.Length == 0 || s.Length == 0)
    {
        return new byte[] { };
    }

    byte[] fCopy = new byte[f.Length - 1];
    byte[] sCopy = new byte[s.Length - 1];

    Array.Copy(f, fCopy, arrayLength - 1);
    Array.Copy(s, sCopy, arrayLength - 1);

    sum = Convert.ToInt16(f[arrayLength - 1] + s[arrayLength - 1]);           

    if (sum > 255)
    {
        r = new byte[] { sum == 510 ? Convert.ToByte(255) : Convert.ToByte(sum % 255 - 1) };

        bool found = false;
        for (int i = arrayLength - 2; i >= 0 && !found; i--)
        {
            if (fCopy[i] < 255)
            {
                fCopy[i] += 1;
                found = true;
            } else if (sCopy[i] < 255)
            {
                sCopy[i] += 1;
                found = true;
            }
        }

        if (!found)
        {
            if (fCopy.Length == 0 || sCopy.Length == 0)
            {
                fCopy = new byte[] { 0 };
                sCopy = new byte[] { 1 };
            }
            else
            {
                fCopy.Concat(new byte[] { 0 });
                sCopy.Concat(new byte[] { 1 });
            }
        }
    } else
    {
        r = new byte[] { Convert.ToByte(sum) };
    }                   

    return AddRecursive(fCopy, sCopy).Concat(r).ToArray();
}

答案 2 :(得分:-1)

  public class AddingThisAddingThat 
    {

        private int carry = 0;        
        public byte[] AddRecursive(byte[] a, byte[] b)
        {
            //Start from bottom of the byte[] array
            a = a.Reverse().ToArray();
            b = b.Reverse().ToArray();
            if (a.Length == 0) return new byte[] { };            
            int tempresult = a[0] + b[0] + carry;
            byte[] z = new byte[]
            { (byte)(tempresult) };
            carry = tempresult / (byte.MaxValue + 1);
            return z.Concat(AddRecursive(a.Skip(1).ToArray(), b.Skip(1).ToArray())).ToArray();
        }


    }

 public void TestSetup()
        {
            addthisaddthat = new AddingThisAddingThat();
        }

        [Test]
        public void Add_UsingARecursiveAlgorithm_ValuesAreAdded()
        {
            //First Test
            byte[] expectedResult = addthisaddthat.AddRecursive(new byte[] { 1, 1, 1 }, new byte[] { 1, 1, 1 }).Reverse().ToArray();
            Assert.That(expectedResult, Is.EqualTo(new byte[] { 2, 2, 2 }));
            //Sec Test
            expectedResult = addthisaddthat.AddRecursive(new byte[] { 1, 1, 255 }, new byte[] { 0, 0, 1 }).Reverse().ToArray();
            Assert.That(expectedResult, Is.EqualTo(new byte[] { 1, 2, 0 }));
            //Third Test
            expectedResult = addthisaddthat.AddRecursive(new byte[] { 255, 255, 255 }, new byte[] { 255, 255, 255 }).Reverse().ToArray();
            Assert.That(expectedResult, Is.EqualTo(new byte[] { 255, 255, 254 }));
        }

        [OneTimeTearDown]
        public void TestTearDown()
        {
            addthisaddthat = null;
        }