Sub EncodeRLE
Dim OriginalBinary() As Byte = FileToBytes(File.DirApp, "test.bin")
Dim FileHex As String
Dim I, RunCount As Int
Dim LastValue As Byte
'Begin with first byte. Runcount of 0.
RunCount = 0
LastValue = OriginalBinary(0)
'Loop through entire file.
For I = 0 To OriginalBinary.Length-1
If OriginalBinary(I) = LastValue Then 'If the value is currently equal to the last one, its considered of the same run.
RunCount = RunCount + 1
If I = (OriginalBinary.Length-1) Then 'We have reached the end of file. Save current value and runlength.
FileHex = FileHex & GetHexWord(RunCount, False)
FileHex = FileHex & HexString(LastValue)
Exit
End If
Else 'The detected value is not of the same run, so lets write the last run and reset.
'Store the run length and value.
FileHex = FileHex & GetHexWord(RunCount, False)
FileHex = FileHex & HexString(LastValue)
'Reset to accomodate for new value.
LastValue = OriginalBinary(I)
RunCount = 1
If I = (OriginalBinary.Length-1) Then 'We have reached the end of file. Save current value and runlength.
Exit
End If
End If
Next
Dim EncodedBinary() As Byte = Convert.HexToBytes(FileHex)
BytesToFile(File.DirApp, "Test.rle", EncodedBinary, False)
End Sub