0

我正在看一个练习,两个加起来两个字节数组

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 提供什么来计算字节数组的总和吗?

4

3 回答 3

1

Here's a Naive code sample to explain what I'm suggesting in my comments (I'm sure that there are better ways of encoding the logic but hopefully it gets the gist across)...

    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);
    }

As I've said, this is essentially assuming that the byte array represents numbers...

So, {1,1,1} is equivalent to 0x10101 or 65793 65793 + 65793 = 131586 or 0x20202 i.e. {2,2,2}

and, {1,1,255} + {0,0,1} is equivalent to 0x101FF + 0x1 or 66047 + 1 = 66048 or 0x10200 i.e. {1,2,0}

于 2013-10-23T10:31:42.497 回答
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();
}
于 2018-03-24T18:56:32.060 回答
-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;
        }
于 2016-09-17T21:57:10.780 回答