Imports System
Public Class Program
Public Shared Sub Main()
Dim byteArray As Byte() = {1, 2, 3, 0, 0, 0, 0}
Dim trimmedArray As Byte() = RemoveTrailingNulls(byteArray)
Console.WriteLine(String.Join(", ", trimmedArray))
End Sub
Public Shared Function RemoveTrailingNulls(ByVal byteArray As Byte()) As Byte()
If byteArray Is Nothing OrElse byteArray.Length = 0 Then Return byteArray
Dim newLength As Integer = byteArray.Length
While newLength > 0 AndAlso byteArray(newLength - 1) = 0
newLength -= 1
End While
Dim trimmedArray As Byte() = New Byte(newLength - 1) {}
Array.Copy(byteArray, trimmedArray, newLength)
Return trimmedArray
End Function
End Class
' run:
'
' 1, 2, 3
'