Skip to content

Instantly share code, notes, and snippets.

@nicholascloud
Last active September 1, 2016 01:23
Show Gist options
  • Save nicholascloud/4152377 to your computer and use it in GitHub Desktop.
Save nicholascloud/4152377 to your computer and use it in GitHub Desktop.
prune trailing null entries in byte array
//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;
}
}
@kstough93
Copy link

Thanks Nicholas, this is just what I needed to handle pruning a byte array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment