1
votes

I'm reading in bytes to a byte array. The numbers are sent in the format <uint16>, inclusive of the '<' and '>' symbols, transmitted in binary format 00111100 XXXXXXXX XXXXXXXX 00111110 where the 'X's make up the 16 bit unsigned int.

I want to remove the '<' and '>' characters, which are always the first and last bytes. This will allow me to convert the 16 bit unsigned int from binary to int.

What is the cleanest way of doing this?

2
You can use Array.Copy. You can specify the starting and end of array to copy from the source. There u can eliminate < and >. Refer this - Olivarsham

2 Answers

6
votes

You could do this simply enough using Linq:

var array = new char[] { '<', '0', '1', '1', '>' };
var trimmedArray = array.Skip( 1 ).Take( array.Count() - 2 ).ToArray();
2
votes

You can use Array.Copy to copy part of one array in to another:

    var source = new int[] { 0, 1, 2, 3, 4 };

    var sourceStartIndex = 1;
    var destinationLength = source.Length - 2;
    var destinationStartIndex = 0;

    var destination = new int[destinationLength];

    Array.Copy(source, sourceStartIndex, destination, destinationStartIndex, destinationLength);

https://dotnetfiddle.net/D0L4XP