ASP.NET添加两字节数组

ASP.NET添加两字节数组,asp.net,bytearray,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 }

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

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

这里有一个简单的代码示例来解释我在评论中的建议(我确信有更好的方法来编码逻辑,但希望它能让人理解要点)

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

所以,{1,1,1}等价于0x10101或65793 65793+65793=131586或0x20202,即{2,2,2}


而且,{1,1255}+{0,0,1}相当于0x101FF+0x1或66047+1=66048或0x10200,即{1,2,0}

您想要的是递归添加两个字节数组的函数。请看下面的示例,它可以满足您的需要:

  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;
        }
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();
}
专用字节[]AddRecursive(字节[]f,字节[]s)
{
整数和;
字节[]r;
int arrayLength=f.长度;
如果(f.Length==0 | | s.Length==0)
{
返回新字节[]{};
}
字节[]fCopy=新字节[f.Length-1];
字节[]=新字节[s.Length-1];
数组.Copy(f,fCopy,arrayLength-1);
阵列。副本(s,透视,阵列长度-1);
总和=转换为16(f[arrayLength-1]+s[arrayLength-1]);
如果(总和>255)
{
r=新字节[]{sum==510?Convert.ToByte(255):Convert.ToByte(sum%255-1)};
bool-found=false;
对于(int i=arrayLength-2;i>=0&&!found;i--)
{
if(fCopy[i]<255)
{
fCopy[i]+=1;
发现=真;
}else if(i]<255)
{
[i]+=1;
发现=真;
}
}
如果(!找到)
{
如果(fCopy.Length==0 | | scope.Length==0)
{
fCopy=新字节[]{0};
新字节【】{1};
}
其他的
{
Concat(新字节[]{0});
Concat(新字节[]{1});
}
}
}否则
{
r=新字节[]{Convert.ToByte(sum)};
}                   
返回AddRecursive(fCopy,sCopy).Concat(r).ToArray();
}

你的第二个结果毫无意义。是的,我就是这么想的,我想知道他们是怎么得出这个结果的。我所知道的只是该方法需要递归地添加字节数组,但在我开始之前,我想知道这里是否有人知道它们是如何得到这些结果的。我理解位置1和2(2和0),但不理解位置0(1)。我希望{2,2,0}甚至可以解释那些位置1,2是如何得到这些结果的?位置1:1+1=2。位置2:255+1=256。因为一个字节可以存储的最大值是255,所以它会将字节溢出回0。但是对于位置0,它有1+1=1
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();
}