Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# how to add byte to byte array

Tags:

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.

like image 749
hs2d Avatar asked Apr 08 '11 06:04

hs2d


Video Answer


1 Answers

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

code:

public byte[] AddByteToArray(byte[] bArray, byte newByte) {     byte[] newArray = new byte[bArray.Length + 1];     bArray.CopyTo(newArray, 1);     newArray[0] = newByte;     return newArray; } 
like image 148
Guffa Avatar answered Sep 29 '22 21:09

Guffa