字节数组的加法 [英] addition of byte array
问题描述
我想在窗口应用程序中添加两个字节数组.
I want to add two byte array in window application.
byte[] b1;
byte[] b2;
我想做:-
I want to do:-
b2=b1+b2;
如何使用字节数组执行此过程.
我正在使用:
为(int i = 0; i< b1.length;> {
b2 [posoftotaldata] + = b1 [i]; //posoftataldata是全局定义的,并且是静态的
posoftotaldata ++;
}
但是在这种情况下,我没有得到b2的确切大小,因为我还全局定义了b1和b2,因此我如何动态定义b2的大小.
可以正常工作,但是会造成大小混乱.
How can i do this process with byte array.
I am use this:
for (int i = 0; i <b1.length;>{
b2[posoftotaldata] += b1[i]; // posoftataldata is define globally and static
posoftotaldata++;
}
But in this i don''t get exact size of b2 because i also defined b1 and b2 globally how i define dynamically size of b2.
This work properly but confusion in size.
推荐答案
一种简单的方法是使用通用列表,并使用AddRange方法将字节数组加在一起- list允许您将数据输出为数组,因此可以得到字节数组.这是一个示例:
A simple way to achieve this is to use a generic list and use the AddRange method to add the byte arrays together - the list allows you to output the data as an array, so you would get a byte array out. Here''s an example:
private byte[] GetBytes(byte[] b1, byte[b2])
{
List<byte> data = new List<byte>();
data.AddRange(b1);
data.AddRange(b2);
return data.ToArray();
}
有很多方法可以达到相同的效果,但这是一个简单,易于调试的版本.
There are many ways to achieve the same effect, but this is a simple, easy to debug version.
这篇关于字节数组的加法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!