Last active
September 1, 2016 01:23
-
-
Save nicholascloud/4152377 to your computer and use it in GitHub Desktop.
prune trailing null entries in byte array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//see http://stackoverflow.com/questions/240258/removing-trailing-nulls-from-byte-array-in-c-sharp | |
static class ByteArrayExtensions { | |
public static Byte[] Prune(this Byte[] bytes) { | |
if (bytes.Length == 0) return bytes; | |
var i = bytes.Length - 1; | |
while (bytes[i] == 0) { | |
i--; | |
} | |
Byte[] copy = new Byte[i + 1]; | |
Array.Copy(bytes, copy, i + 1); | |
return copy; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks Nicholas, this is just what I needed to handle pruning a byte array.