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?

1
  • 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 Commented Feb 23, 2016 at 11:06

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

1 Comment

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

2 Comments

That works - one note is that it's "Array.Copy" not "Array.Clone"
Erk, yep, I'll fix that!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.