c#如何将字节添加到字节数组 [英] c# how to add byte to byte array

查看:28
本文介绍了c#如何将字节添加到字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在现有字节数组的开头添加一个字节?我的目标是使数组的长度为 3 个字节到 4 个字节.所以这就是为什么我需要在它的开头添加 00 填充.

How to add a byte to the beginning of an existing byte array? My goal is to make array what's 3 bytes long to 4 bytes. So that's why I need to add 00 padding in the beginning of it.

推荐答案

你不能那样做.无法调整数组大小.您必须创建一个新数组并将数据复制到其中:

You can't do that. It's not possible to resize an array. You have to create a new array and copy the data to it:

bArray = AddByteToArray(bArray,  newByte);

代码:

public byte[] AddByteToArray(byte[] bArray, byte newByte)
{
    byte[] newArray = new byte[bArray.Length + 1];
    bArray.CopyTo(newArray, 1);
    newArray[0] = newByte;
    return newArray;
}

这篇关于c#如何将字节添加到字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆