Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C# how can I truncate a byte[] array

Tags:

c#

I have a byte[] array of one size, and I would like to truncate it into a smaller array?

I just want to chop the end off.

like image 257
David Sykes Avatar asked Nov 21 '11 15:11

David Sykes


People also ask

What does '?' Mean in C?

Most likely the '?' is the ternary operator. Its grammar is: RESULT = (COND) ? ( STATEMEN IF TRUE) : (STATEMENT IF FALSE) It is a nice shorthand for the typical if-else statement: if (COND) { RESULT = (STATEMENT IF TRUE); } else { RESULT = (STATEMENT IF FALSE);

What does |= mean in C++?

|= just assigns the bitwise OR of a variable with another to the one on the LHS.

What is an operator in C?

C operators are one of the features in C which has symbols that can be used to perform mathematical, relational, bitwise, conditional, or logical manipulations. The C programming language has a lot of built-in operators to perform various tasks as per the need of the program.


2 Answers

Arrays are fixed-size in C# (.NET).

You'll have to copy the contents to a new one.

byte[] sourceArray = ... byte[] truncArray = new byte[10];  Array.Copy(sourceArray , truncArray , truncArray.Length); 
like image 150
Henk Holterman Avatar answered Sep 20 '22 17:09

Henk Holterman


You could use Array.Resize, but all this really does is make a truncated copy of the original array and then replaces the original array with the new one.

like image 43
LukeH Avatar answered Sep 20 '22 17:09

LukeH