Edit January 5th, 2025. Added unit tests and separate class
Option Strict On
Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Friend NotInheritable Class FormMain
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim byteArray As Byte() = New List(Of Byte)() From {103, 100, 0, 51, 172, 217, 0, 113, 1, 83, 229, 188, 4, 64, 0, 0, 3, 0, 64, 0, 0, 15, 3, 198, 12, 101, 128}.ToArray()
Dim result As (Byte(), Integer) = Decoder.ExtractRelevantBytesWithout3(byteArray, 106, 32)
Dim result2 As (Byte(), Integer) = Decoder.ExtractRelevantBytesWithout3(Array.Empty(Of Byte)(), 220, 32)
End Sub
End Class
Option Strict On
Imports System
Imports System.Collections.Generic
Public Class Decoder
Public Shared Function ExtractRelevantBytesWithout3(byteArray As Byte(), bitStartPosition As Integer, bitCount As Integer) As (Byte(), numberOfSkippedBytes As Integer)
If bitStartPosition > byteArray.Length * 8 Then
Throw New ArgumentException("The bit start position exceeds the size of the byte array.", NameOf(bitStartPosition))
End If
Dim result As New List(Of Byte)()
Dim bitsRemaining As Integer = bitCount
Dim currentBitPosition As Integer = bitStartPosition
Dim numberOfSkippedBytes As Integer = 0
Dim increaseOnce As Boolean = True
While bitsRemaining > 0 AndAlso currentBitPosition \ 8 < byteArray.Length
Dim currentByteIndex As Integer = currentBitPosition \ 8
Dim bitIndexWithinByte As Integer = currentBitPosition Mod 8
If bitIndexWithinByte > 0 AndAlso increaseOnce Then
bitsRemaining += 8
increaseOnce = False
End If
Dim currentByte As Byte = byteArray(currentByteIndex)
If currentByte = CByte(3) AndAlso bitsRemaining > 8 Then ' bitsRemaining > 8 means: Do not skip if the 3 is not read completely.
currentBitPosition += 8 ' Skip to the next byte
numberOfSkippedBytes += 1
Continue While
End If
result.Add(currentByte)
currentBitPosition += 8
bitsRemaining -= Math.Min(bitsRemaining, 8) ' Deduct processed bits
End While
Return (result.ToArray(), numberOfSkippedBytes)
End Function
End Class
Unit tests (in C# due to separate class library)
usingOption NUnit.Framework;Strict On
usingImports System;System
usingImports System.Collections.Generic;
Generic
namespaceImports ExtractRelevantBytesWithout3System.Tests
{
public class Remove3sTest
{
[SetUp]
public void Setup()
{
}
[Test]
public void ByteArrayDoesNotContain3_ReturnsOriginalArray()
{
// Arrange
byte[] originalArray = new List<byte>() { 103, 100, 0, 51, 172, 217, 0, 113, 1, 83, 229, 188, 4, 64, 0, 0, 0, 64, 0, 0, 15, 198, 12, 101, 128 }Windows.ToArray();
Forms
Friend NotInheritable //Class ActFormMain
Private Sub Button1_Click(byte[], int skippedBytes) resultsender =As Decoder.ExtractRelevantBytesWithout3(originalArrayObject, bitStartPosition: 106,e bitCount:As 32EventArgs);
//Handles AssertButton1.Click
Dim byteArray Assert.That(result.Item1,As Is.EqualToByte(new byte[] { 64, 0, 0, 0, 64 }));
= New Assert.ThatList(result.skippedBytes,Of Is.EqualTo(0)Byte);
}
[Test]
public void ByteArrayContains3_Removes3()
{
// Arrange
byte[] originalArray = new List<byte>()From {103, 100, 0, 51, 172, 217, 0, 113, 1, 83, 229, 188, 4, 64, 0, 0, 3, 0, 64, 0, 0, 15, 3, 198, 12, 101, 128}.ToArray();
// Act
Dim result As (byte[]Byte(), int skippedBytesInteger) result = Decoder.ExtractRelevantBytesWithout3(originalArraybyteArray, bitStartPosition: 106, bitCount: 32);
//End AssertSub
Private Shared Function ExtractRelevantBytesWithout3(byteArray As Assert.ThatByte(result.Item1), Is.EqualTo(new byte[] { 64,bitStartPosition 0,As 0Integer, 0,bitCount 64As })Integer);
As Assert.That(result.skippedBytes, Is.EqualToByte(1));
}
[Test]
, publicnumberOfSkippedBytes voidAs ByteArrayContains3_3isPartiallyRead_ReturnsArrayWith3(Integer)
{
// Arrange
byte[]Dim originalArrayresult =As newNew List<byte>List() { 103, 100, 0, 51, 172, 217, 0, 113, 1, 83, 229, 188, 4, 64, 0, 0, 3, 0, 64, 0, 0, 15, 3, 198, 12, 101, 128Of }.ToArrayByte)();
Dim bitsRemaining As Integer //= ActbitCount
(byte[],Dim intcurrentBitPosition skippedBytes)As resultInteger = Decoder.ExtractRelevantBytesWithout3(originalArray, bitStartPosition: 146, bitCount: 32);
Dim numberOfSkippedBytes As Integer //= Assert0
Assert.That(result.Item1, Is.EqualTo(new byte[] { 64,Dim 0,increaseOnce 0,As 15,Boolean 3= }));True
While bitsRemaining Assert.That(result.skippedBytes,> Is.EqualTo(0));
AndAlso currentBitPosition \ 8 < }
byteArray.Length
/* Edge Cases */
Dim currentByteIndex As Integer = currentBitPosition \ [Test]8
public void ByteArrayIsEmpty_ReturnsEmpty()
{
Dim bitIndexWithinByte As Integer = currentBitPosition //Mod Arrange8
byte[] originalArray = Array.Empty<byte>();
If bitIndexWithinByte > 0 AndAlso //increaseOnce ActThen
(byte[], int skippedBytes) result = Decoder.ExtractRelevantBytesWithout3(originalArray, bitStartPosition: 0,bitsRemaining bitCount:+= 1);
8
// Assert
increaseOnce Assert.That(result.Item1,= Is.Empty);False
Assert.That(result.skippedBytes, Is.EqualTo(0));
End }If
[Test]
Dim currentByte As publicByte void= BitPositionGreaterThanArraySize_ThrowsArgumentExceptionbyteArray(currentByteIndex)
{
// Arrange
byte[]If originalArraycurrentByte = new List<byte>CByte(3) { 103, 100, 0, 51, 172, 217, 0, 113,AndAlso 1,bitsRemaining 83,> 229,8 188,Then 4,' 64,bitsRemaining 0,> 0,8 3,means: 0,Do 64,not 0,skip 0,if 15,the 3, 198, 12,is 101,not 128read }completely.ToArray();
// Act & Assert
currentBitPosition += 8 ' Skip to the Assert.Throws<ArgumentException>(()next =>byte
Decoder.ExtractRelevantBytesWithout3(originalArray, bitStartPosition: 217, bitCount: 32));
}
numberOfSkippedBytes += [Test]1
public void LargeArrayPerformanceTest()
Continue {While
//End ArrangeIf
byte[] largeArray = new byte[1000000];
for (int i = 0; i < largeArrayresult.Length; i++)
{
largeArray[i] = (byte)Add(i % 256currentByte);
}
currentBitPosition //+= Act8
(byte[], int skippedBytes) resultbitsRemaining -= DecoderMath.ExtractRelevantBytesWithout3Min(largeArray, bitStartPosition: 0bitsRemaining, bitCount: largeArray.Length * 8);
' Deduct //processed Assertbits
Assert.That(result.Item1.Length >End 0);While
Return Assert.That(result.skippedBytes, Is.GreaterThanToArray(0), numberOfSkippedBytes);
End }Function
}
}End Class
Edit January 5th, 2025. Added unit tests and separate class
Option Strict On
Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Friend NotInheritable Class FormMain
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim byteArray As Byte() = New List(Of Byte)() From {103, 100, 0, 51, 172, 217, 0, 113, 1, 83, 229, 188, 4, 64, 0, 0, 3, 0, 64, 0, 0, 15, 3, 198, 12, 101, 128}.ToArray()
Dim result As (Byte(), Integer) = Decoder.ExtractRelevantBytesWithout3(byteArray, 106, 32)
Dim result2 As (Byte(), Integer) = Decoder.ExtractRelevantBytesWithout3(Array.Empty(Of Byte)(), 220, 32)
End Sub
End Class
Option Strict On
Imports System
Imports System.Collections.Generic
Public Class Decoder
Public Shared Function ExtractRelevantBytesWithout3(byteArray As Byte(), bitStartPosition As Integer, bitCount As Integer) As (Byte(), numberOfSkippedBytes As Integer)
If bitStartPosition > byteArray.Length * 8 Then
Throw New ArgumentException("The bit start position exceeds the size of the byte array.", NameOf(bitStartPosition))
End If
Dim result As New List(Of Byte)()
Dim bitsRemaining As Integer = bitCount
Dim currentBitPosition As Integer = bitStartPosition
Dim numberOfSkippedBytes As Integer = 0
Dim increaseOnce As Boolean = True
While bitsRemaining > 0 AndAlso currentBitPosition \ 8 < byteArray.Length
Dim currentByteIndex As Integer = currentBitPosition \ 8
Dim bitIndexWithinByte As Integer = currentBitPosition Mod 8
If bitIndexWithinByte > 0 AndAlso increaseOnce Then
bitsRemaining += 8
increaseOnce = False
End If
Dim currentByte As Byte = byteArray(currentByteIndex)
If currentByte = CByte(3) AndAlso bitsRemaining > 8 Then ' bitsRemaining > 8 means: Do not skip if the 3 is not read completely.
currentBitPosition += 8 ' Skip to the next byte
numberOfSkippedBytes += 1
Continue While
End If
result.Add(currentByte)
currentBitPosition += 8
bitsRemaining -= Math.Min(bitsRemaining, 8) ' Deduct processed bits
End While
Return (result.ToArray(), numberOfSkippedBytes)
End Function
End Class
Unit tests (in C# due to separate class library)
using NUnit.Framework;
using System;
using System.Collections.Generic;
namespace ExtractRelevantBytesWithout3.Tests
{
public class Remove3sTest
{
[SetUp]
public void Setup()
{
}
[Test]
public void ByteArrayDoesNotContain3_ReturnsOriginalArray()
{
// Arrange
byte[] originalArray = new List<byte>() { 103, 100, 0, 51, 172, 217, 0, 113, 1, 83, 229, 188, 4, 64, 0, 0, 0, 64, 0, 0, 15, 198, 12, 101, 128 }.ToArray();
// Act
(byte[], int skippedBytes) result = Decoder.ExtractRelevantBytesWithout3(originalArray, bitStartPosition: 106, bitCount: 32);
// Assert
Assert.That(result.Item1, Is.EqualTo(new byte[] { 64, 0, 0, 0, 64 }));
Assert.That(result.skippedBytes, Is.EqualTo(0));
}
[Test]
public void ByteArrayContains3_Removes3()
{
// Arrange
byte[] originalArray = new List<byte>() {103, 100, 0, 51, 172, 217, 0, 113, 1, 83, 229, 188, 4, 64, 0, 0, 3, 0, 64, 0, 0, 15, 3, 198, 12, 101, 128}.ToArray();
// Act
(byte[], int skippedBytes) result = Decoder.ExtractRelevantBytesWithout3(originalArray, bitStartPosition: 106, bitCount: 32);
// Assert
Assert.That(result.Item1, Is.EqualTo(new byte[] { 64, 0, 0, 0, 64 }));
Assert.That(result.skippedBytes, Is.EqualTo(1));
}
[Test]
public void ByteArrayContains3_3isPartiallyRead_ReturnsArrayWith3()
{
// Arrange
byte[] originalArray = new List<byte>() { 103, 100, 0, 51, 172, 217, 0, 113, 1, 83, 229, 188, 4, 64, 0, 0, 3, 0, 64, 0, 0, 15, 3, 198, 12, 101, 128 }.ToArray();
// Act
(byte[], int skippedBytes) result = Decoder.ExtractRelevantBytesWithout3(originalArray, bitStartPosition: 146, bitCount: 32);
// Assert
Assert.That(result.Item1, Is.EqualTo(new byte[] { 64, 0, 0, 15, 3 }));
Assert.That(result.skippedBytes, Is.EqualTo(0));
}
/* Edge Cases */
[Test]
public void ByteArrayIsEmpty_ReturnsEmpty()
{
// Arrange
byte[] originalArray = Array.Empty<byte>();
// Act
(byte[], int skippedBytes) result = Decoder.ExtractRelevantBytesWithout3(originalArray, bitStartPosition: 0, bitCount: 1);
// Assert
Assert.That(result.Item1, Is.Empty);
Assert.That(result.skippedBytes, Is.EqualTo(0));
}
[Test]
public void BitPositionGreaterThanArraySize_ThrowsArgumentException()
{
// Arrange
byte[] originalArray = new List<byte>() { 103, 100, 0, 51, 172, 217, 0, 113, 1, 83, 229, 188, 4, 64, 0, 0, 3, 0, 64, 0, 0, 15, 3, 198, 12, 101, 128 }.ToArray();
// Act & Assert
Assert.Throws<ArgumentException>(() =>
Decoder.ExtractRelevantBytesWithout3(originalArray, bitStartPosition: 217, bitCount: 32));
}
[Test]
public void LargeArrayPerformanceTest()
{
// Arrange
byte[] largeArray = new byte[1000000];
for (int i = 0; i < largeArray.Length; i++)
{
largeArray[i] = (byte)(i % 256);
}
// Act
(byte[], int skippedBytes) result = Decoder.ExtractRelevantBytesWithout3(largeArray, bitStartPosition: 0, bitCount: largeArray.Length * 8);
// Assert
Assert.That(result.Item1.Length > 0);
Assert.That(result.skippedBytes, Is.GreaterThan(0));
}
}
}
Option Strict On
Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Friend NotInheritable Class FormMain
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim byteArray As Byte() = New List(Of Byte)() From {103, 100, 0, 51, 172, 217, 0, 113, 1, 83, 229, 188, 4, 64, 0, 0, 3, 0, 64, 0, 0, 15, 3, 198, 12, 101, 128}.ToArray()
Dim result As (Byte(), Integer) = ExtractRelevantBytesWithout3(byteArray, 106, 32)
End Sub
Private Shared Function ExtractRelevantBytesWithout3(byteArray As Byte(), bitStartPosition As Integer, bitCount As Integer) As (Byte(), numberOfSkippedBytes As Integer)
Dim result As New List(Of Byte)()
Dim bitsRemaining As Integer = bitCount
Dim currentBitPosition As Integer = bitStartPosition
Dim numberOfSkippedBytes As Integer = 0
Dim increaseOnce As Boolean = True
While bitsRemaining > 0 AndAlso currentBitPosition \ 8 < byteArray.Length
Dim currentByteIndex As Integer = currentBitPosition \ 8
Dim bitIndexWithinByte As Integer = currentBitPosition Mod 8
If bitIndexWithinByte > 0 AndAlso increaseOnce Then
bitsRemaining += 8
increaseOnce = False
End If
Dim currentByte As Byte = byteArray(currentByteIndex)
If currentByte = CByte(3) AndAlso bitsRemaining > 8 Then ' bitsRemaining > 8 means: Do not skip if the 3 is not read completely.
currentBitPosition += 8 ' Skip to the next byte
numberOfSkippedBytes += 1
Continue While
End If
result.Add(currentByte)
currentBitPosition += 8
bitsRemaining -= Math.Min(bitsRemaining, 8) ' Deduct processed bits
End While
Return (result.ToArray(), numberOfSkippedBytes)
End Function
End Class
Edit January 5th, 2025. Added unit tests and separate class
Option Strict On
Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Friend NotInheritable Class FormMain
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim byteArray As Byte() = New List(Of Byte)() From {103, 100, 0, 51, 172, 217, 0, 113, 1, 83, 229, 188, 4, 64, 0, 0, 3, 0, 64, 0, 0, 15, 3, 198, 12, 101, 128}.ToArray()
Dim result As (Byte(), Integer) = Decoder.ExtractRelevantBytesWithout3(byteArray, 106, 32)
Dim result2 As (Byte(), Integer) = Decoder.ExtractRelevantBytesWithout3(Array.Empty(Of Byte)(), 220, 32)
End Sub
End Class
Option Strict On
Imports System
Imports PrivateSystem.Collections.Generic
Public Class Decoder
Public Shared Function ExtractRelevantBytesWithout3(byteArray As Byte(), bitStartPosition As Integer, bitCount As Integer) As (Byte(), numberOfSkippedBytes As Integer)
If bitStartPosition > byteArray.Length * 8 Then
Throw New ArgumentException("The bit start position exceeds the size of the byte array.", NameOf(bitStartPosition))
End If
Dim result As New List(Of Byte)()
Dim bitsRemaining As Integer = bitCount
Dim currentBitPosition As Integer = bitStartPosition
Dim numberOfSkippedBytes As Integer = 0
Dim increaseOnce As Boolean = True
While bitsRemaining > 0 AndAlso currentBitPosition \ 8 < byteArray.Length
Dim currentByteIndex As Integer = currentBitPosition \ 8
Dim bitIndexWithinByte As Integer = currentBitPosition Mod 8
If bitIndexWithinByte > 0 AndAlso increaseOnce Then
bitsRemaining += 8
increaseOnce = False
End If
Dim currentByte As Byte = byteArray(currentByteIndex)
If currentByte = CByte(3) AndAlso bitsRemaining > 8 Then ' bitsRemaining > 8 means: Do not skip if the 3 is not read completely.
currentBitPosition += 8 ' Skip to the next byte
numberOfSkippedBytes += 1
Continue While
End If
result.Add(currentByte)
currentBitPosition += 8
bitsRemaining -= Math.Min(bitsRemaining, 8) ' Deduct processed bits
End While
Return (result.ToArray(), numberOfSkippedBytes)
End Function
End Class
Unit tests (in C# due to separate class library)
using NUnit.Framework;
using System;
using System.Collections.Generic;
namespace ExtractRelevantBytesWithout3.Tests
{
public class Remove3sTest
{
[SetUp]
public void Setup()
{
}
[Test]
public void ByteArrayDoesNotContain3_ReturnsOriginalArray()
{
// Arrange
byte[] originalArray = new List<byte>() { 103, 100, 0, 51, 172, 217, 0, 113, 1, 83, 229, 188, 4, 64, 0, 0, 0, 64, 0, 0, 15, 198, 12, 101, 128 }.ToArray();
// Act
(byte[], int skippedBytes) result = Decoder.ExtractRelevantBytesWithout3(originalArray, bitStartPosition: 106, bitCount: 32);
// Assert
Assert.That(result.Item1, Is.EqualTo(new byte[] { 64, 0, 0, 0, 64 }));
Assert.That(result.skippedBytes, Is.EqualTo(0));
}
[Test]
public void ByteArrayContains3_Removes3()
{
// Arrange
byte[] originalArray = new List<byte>() { 103, 100, 0, 51, 172, 217, 0, 113, 1, 83, 229, 188, 4, 64, 0, 0, 3, 0, 64, 0, 0, 15, 3, 198, 12, 101, 128 }.ToArray();
// Act
(byte[], int skippedBytes) result = Decoder.ExtractRelevantBytesWithout3(originalArray, bitStartPosition: 106, bitCount: 32);
// Assert
Assert.That(result.Item1, Is.EqualTo(new byte[] { 64, 0, 0, 0, 64 }));
Assert.That(result.skippedBytes, Is.EqualTo(1));
}
[Test]
public void ByteArrayContains3_3isPartiallyRead_ReturnsArrayWith3()
{
// Arrange
byte[] originalArray = new List<byte>() { 103, 100, 0, 51, 172, 217, 0, 113, 1, 83, 229, 188, 4, 64, 0, 0, 3, 0, 64, 0, 0, 15, 3, 198, 12, 101, 128 }.ToArray();
// Act
(byte[], int skippedBytes) result = Decoder.ExtractRelevantBytesWithout3(originalArray, bitStartPosition: 146, bitCount: 32);
// Assert
Assert.That(result.Item1, Is.EqualTo(new byte[] { 64, 0, 0, 15, 3 }));
Assert.That(result.skippedBytes, Is.EqualTo(0));
}
/* Edge Cases */
[Test]
public void ByteArrayIsEmpty_ReturnsEmpty()
{
// Arrange
byte[] originalArray = Array.Empty<byte>();
// Act
(byte[], int skippedBytes) result = Decoder.ExtractRelevantBytesWithout3(originalArray, bitStartPosition: 0, bitCount: 1);
// Assert
Assert.That(result.Item1, Is.Empty);
Assert.That(result.skippedBytes, Is.EqualTo(0));
}
[Test]
public void BitPositionGreaterThanArraySize_ThrowsArgumentException()
{
// Arrange
byte[] originalArray = new List<byte>() { 103, 100, 0, 51, 172, 217, 0, 113, 1, 83, 229, 188, 4, 64, 0, 0, 3, 0, 64, 0, 0, 15, 3, 198, 12, 101, 128 }.ToArray();
// Act & Assert
Assert.Throws<ArgumentException>(() =>
Decoder.ExtractRelevantBytesWithout3(originalArray, bitStartPosition: 217, bitCount: 32));
}
[Test]
public void LargeArrayPerformanceTest()
{
// Arrange
byte[] largeArray = new byte[1000000];
for (int i = 0; i < largeArray.Length; i++)
{
largeArray[i] = (byte)(i % 256);
}
// Act
(byte[], int skippedBytes) result = Decoder.ExtractRelevantBytesWithout3(largeArray, bitStartPosition: 0, bitCount: largeArray.Length * 8);
// Assert
Assert.That(result.Item1.Length > 0);
Assert.That(result.skippedBytes, Is.GreaterThan(0));
}
}
}
Option Strict On
Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Friend NotInheritable Class FormMain
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim byteArray As Byte() = New List(Of Byte)() From {103, 100, 0, 51, 172, 217, 0, 113, 1, 83, 229, 188, 4, 64, 0, 0, 3, 0, 64, 0, 0, 15, 3, 198, 12, 101, 128}.ToArray()
Dim result As (Byte(), Integer) = ExtractRelevantBytesWithout3(byteArray, 106, 32)
End Sub
Private Shared Function ExtractRelevantBytesWithout3(byteArray As Byte(), bitStartPosition As Integer, bitCount As Integer) As (Byte(), numberOfSkippedBytes As Integer)
Dim result As New List(Of Byte)()
Dim bitsRemaining As Integer = bitCount
Dim currentBitPosition As Integer = bitStartPosition
Dim numberOfSkippedBytes As Integer = 0
Dim increaseOnce As Boolean = True
While bitsRemaining > 0 AndAlso currentBitPosition \ 8 < byteArray.Length
Dim currentByteIndex As Integer = currentBitPosition \ 8
Dim bitIndexWithinByte As Integer = currentBitPosition Mod 8
If bitIndexWithinByte > 0 AndAlso increaseOnce Then
bitsRemaining += 8
increaseOnce = False
End If
Dim currentByte As Byte = byteArray(currentByteIndex)
If currentByte = CByte(3) AndAlso bitsRemaining > 8 Then ' bitsRemaining > 8 means: Do not skip if the 3 is not read completely.
currentBitPosition += 8 ' Skip to the next byte
numberOfSkippedBytes += 1
Continue While
End If
result.Add(currentByte)
currentBitPosition += 8
bitsRemaining -= Math.Min(bitsRemaining, 8) ' Deduct processed bits
End While
Return (result.ToArray(), numberOfSkippedBytes)
End Function
End Class
Edit January 5th, 2025. Added unit tests and separate class
Option Strict On
Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Friend NotInheritable Class FormMain
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim byteArray As Byte() = New List(Of Byte)() From {103, 100, 0, 51, 172, 217, 0, 113, 1, 83, 229, 188, 4, 64, 0, 0, 3, 0, 64, 0, 0, 15, 3, 198, 12, 101, 128}.ToArray()
Dim result As (Byte(), Integer) = Decoder.ExtractRelevantBytesWithout3(byteArray, 106, 32)
Dim result2 As (Byte(), Integer) = Decoder.ExtractRelevantBytesWithout3(Array.Empty(Of Byte)(), 220, 32)
End Sub
End Class
Option Strict On
Imports System
Imports System.Collections.Generic
Public Class Decoder
Public Shared Function ExtractRelevantBytesWithout3(byteArray As Byte(), bitStartPosition As Integer, bitCount As Integer) As (Byte(), numberOfSkippedBytes As Integer)
If bitStartPosition > byteArray.Length * 8 Then
Throw New ArgumentException("The bit start position exceeds the size of the byte array.", NameOf(bitStartPosition))
End If
Dim result As New List(Of Byte)()
Dim bitsRemaining As Integer = bitCount
Dim currentBitPosition As Integer = bitStartPosition
Dim numberOfSkippedBytes As Integer = 0
Dim increaseOnce As Boolean = True
While bitsRemaining > 0 AndAlso currentBitPosition \ 8 < byteArray.Length
Dim currentByteIndex As Integer = currentBitPosition \ 8
Dim bitIndexWithinByte As Integer = currentBitPosition Mod 8
If bitIndexWithinByte > 0 AndAlso increaseOnce Then
bitsRemaining += 8
increaseOnce = False
End If
Dim currentByte As Byte = byteArray(currentByteIndex)
If currentByte = CByte(3) AndAlso bitsRemaining > 8 Then ' bitsRemaining > 8 means: Do not skip if the 3 is not read completely.
currentBitPosition += 8 ' Skip to the next byte
numberOfSkippedBytes += 1
Continue While
End If
result.Add(currentByte)
currentBitPosition += 8
bitsRemaining -= Math.Min(bitsRemaining, 8) ' Deduct processed bits
End While
Return (result.ToArray(), numberOfSkippedBytes)
End Function
End Class
Unit tests (in C# due to separate class library)
using NUnit.Framework;
using System;
using System.Collections.Generic;
namespace ExtractRelevantBytesWithout3.Tests
{
public class Remove3sTest
{
[SetUp]
public void Setup()
{
}
[Test]
public void ByteArrayDoesNotContain3_ReturnsOriginalArray()
{
// Arrange
byte[] originalArray = new List<byte>() { 103, 100, 0, 51, 172, 217, 0, 113, 1, 83, 229, 188, 4, 64, 0, 0, 0, 64, 0, 0, 15, 198, 12, 101, 128 }.ToArray();
// Act
(byte[], int skippedBytes) result = Decoder.ExtractRelevantBytesWithout3(originalArray, bitStartPosition: 106, bitCount: 32);
// Assert
Assert.That(result.Item1, Is.EqualTo(new byte[] { 64, 0, 0, 0, 64 }));
Assert.That(result.skippedBytes, Is.EqualTo(0));
}
[Test]
public void ByteArrayContains3_Removes3()
{
// Arrange
byte[] originalArray = new List<byte>() { 103, 100, 0, 51, 172, 217, 0, 113, 1, 83, 229, 188, 4, 64, 0, 0, 3, 0, 64, 0, 0, 15, 3, 198, 12, 101, 128 }.ToArray();
// Act
(byte[], int skippedBytes) result = Decoder.ExtractRelevantBytesWithout3(originalArray, bitStartPosition: 106, bitCount: 32);
// Assert
Assert.That(result.Item1, Is.EqualTo(new byte[] { 64, 0, 0, 0, 64 }));
Assert.That(result.skippedBytes, Is.EqualTo(1));
}
[Test]
public void ByteArrayContains3_3isPartiallyRead_ReturnsArrayWith3()
{
// Arrange
byte[] originalArray = new List<byte>() { 103, 100, 0, 51, 172, 217, 0, 113, 1, 83, 229, 188, 4, 64, 0, 0, 3, 0, 64, 0, 0, 15, 3, 198, 12, 101, 128 }.ToArray();
// Act
(byte[], int skippedBytes) result = Decoder.ExtractRelevantBytesWithout3(originalArray, bitStartPosition: 146, bitCount: 32);
// Assert
Assert.That(result.Item1, Is.EqualTo(new byte[] { 64, 0, 0, 15, 3 }));
Assert.That(result.skippedBytes, Is.EqualTo(0));
}
/* Edge Cases */
[Test]
public void ByteArrayIsEmpty_ReturnsEmpty()
{
// Arrange
byte[] originalArray = Array.Empty<byte>();
// Act
(byte[], int skippedBytes) result = Decoder.ExtractRelevantBytesWithout3(originalArray, bitStartPosition: 0, bitCount: 1);
// Assert
Assert.That(result.Item1, Is.Empty);
Assert.That(result.skippedBytes, Is.EqualTo(0));
}
[Test]
public void BitPositionGreaterThanArraySize_ThrowsArgumentException()
{
// Arrange
byte[] originalArray = new List<byte>() { 103, 100, 0, 51, 172, 217, 0, 113, 1, 83, 229, 188, 4, 64, 0, 0, 3, 0, 64, 0, 0, 15, 3, 198, 12, 101, 128 }.ToArray();
// Act & Assert
Assert.Throws<ArgumentException>(() =>
Decoder.ExtractRelevantBytesWithout3(originalArray, bitStartPosition: 217, bitCount: 32));
}
[Test]
public void LargeArrayPerformanceTest()
{
// Arrange
byte[] largeArray = new byte[1000000];
for (int i = 0; i < largeArray.Length; i++)
{
largeArray[i] = (byte)(i % 256);
}
// Act
(byte[], int skippedBytes) result = Decoder.ExtractRelevantBytesWithout3(largeArray, bitStartPosition: 0, bitCount: largeArray.Length * 8);
// Assert
Assert.That(result.Item1.Length > 0);
Assert.That(result.skippedBytes, Is.GreaterThan(0));
}
}
}
I have written a VB.NET function that takes a byte array, a starting position (in bits), and a number of bits as input. The goal is to remove any occurrences of 3 from the specified range being searched. The function should return the new byte array with the specified length and the information about how many bytes were skipped.
I have written a VB.NET function that takes a byte array, a starting position, and a number of bits as input. The goal is to remove any occurrences of 3 from the specified range being searched. The function should return the new byte array with the specified length and the information about how many bytes were skipped.
I have written a VB.NET function that takes a byte array, a starting position (in bits), and a number of bits as input. The goal is to remove any occurrences of 3 from the specified range being searched. The function should return the new byte array with the specified length and the information about how many bytes were skipped.