简体   繁体   English

C#合并两个2D字节数组(以零填充)

[英]C# Combine two 2D byte arrays (Filling in zeros)

I have two 2D byte arrays that I want to merge. 我有两个要合并的2D字节数组。 For example: 例如:

2D array1:         2D array2:           Combined array3:
[1][0][1]          [0][1][0]            [1][1][1]
[0][0][0]          [1][0][1]     =>     [1][0][1]
[1][0][1]          [0][1][0]            [1][1][1]

I tried like this: 我这样尝试过:

for(int i = 0; i < array3.GetLength(0); i++)
{
    for(int j = 0; j < array3.GetLength(1); j++)
    {
        // Missing a cast
        array3[i, j] = array1[i, j] + array2[i, j];
    }
}

The line under the comment says it cannot convert to byte from int . 注释下面的行说它不能从int转换为byte I was reading this: byte + byte = int... why? 我在读: 字节+字节=整数...为什么? , but still couldn't figure out what I want to accomplish. ,但仍然不知道我要完成什么。

How would I go about combining them? 我将如何结合它们? Thanks in advance! 提前致谢!

Try this to get the output you want: 尝试此操作以获得所需的输出:

for(int i = 0; i < array3.GetLength(0); i++)
{
    for(int j = 0; j < array3.GetLength(1); j++)
    {
        // All you need to do is cast the int to a byte.
        array3[i, j] = (byte)((array1[i, j] == 0) ? array2[i, j] : array1[i, j]);
    }
}

Adding array1[i, j] and array2[i, j] could give you 2 , which is not wat you want in your example. array1[i, j]array2[i, j]可以得到2 ,这在您的示例中并不理想。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM