2

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?

BenAdamson
  • 625
  • 3
  • 10
  • 19
  • 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](http://stackoverflow.com/questions/733243/how-to-copy-part-of-an-array-to-another-array-in-c) – Olivarsham Feb 23 '16 at 11:06

2 Answers2

6

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();
LordWilmore
  • 2,829
  • 2
  • 25
  • 30
  • 1
    Never know this one is achievable too. As I'm currently using hex string and using it like `string HexTLV = string.Concat(HexResponse.Skip(2).Take(HexResponse.Length - 4));` – Luiey Jul 13 '20 at 01:52
2

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