Hi, I am now trying to convert your C++ code to vb.net. I mostly succeeded but there is a bug causing too much data to be written to the output file. EDIT: FIXED. (The variable "a" should've been converted to a byte before writing it to a file.)
Code:
Option Compare Binary
Option Explicit On
Option Infer Off
Option Strict On
Imports System
Imports System.Convert
Imports System.Environment
Imports System.IO
Imports System.Math
Public Module Module1
Dim lzw(65536, 2) As Integer
Public Sub Main()
Dim a As New Integer
Dim Bit As Integer = 0
Dim BitCount As Integer = 9 'wordsize
Dim Character As New Byte 'c
Dim d As Integer = 0
Dim i_lzw As Integer = &H102%
Dim InputFile As New BinaryReader(File.Open(GetCommandLineArgs(1), FileMode.Open)) 'f
Dim j As New Integer
Dim OutputFile As New BinaryWriter(File.Open(GetCommandLineArgs(2), FileMode.Create)) 'g
Dim pd As Integer = -1
InputFile.BaseStream.Seek(10, SeekOrigin.Begin)
For j = &H0% To &HFF%
lzw(j, 0) = j
lzw(j, 1) = -1
Next j
lzw(j, 0) = -1
lzw(j, 1) = -1
j += 1
lzw(j, 0) = -1
lzw(j, 1) = -1
Do Until InputFile.BaseStream.Position >= InputFile.BaseStream.Length
Character = InputFile.ReadByte()
For j = 0 To 7
d = d Or ((Character >> j) And &H1%) << Bit
Bit += 1
If Bit >= BitCount Then
If d = &H101% Then Exit Do 'EOF?
If d = &H100% Then
BitCount = 9
i_lzw = &H102%
pd = -1
Else
If pd = -1 Then
a = d
Else
a = Dump(If(d < i_lzw, d, pd), OutputFile)
lzw(i_lzw, 0) = pd
lzw(i_lzw, 1) = a
a = If(d < i_lzw, -1, a)
i_lzw += 1
If i_lzw = (1 << BitCount) Then BitCount += Abs(CInt(BitCount < 12))
End If
If a >= 0 Then OutputFile.Write(ToByte(a))
pd = d
End If
Bit = 0
d = 0
End If
Next j
Loop
End Sub
Private Function Dump(d As Integer, OutputFile As BinaryWriter) As Integer
Dim c As New Integer
If d < &H100% Then
c = d
OutputFile.Write(ToByte(c))
Else
c = Dump(lzw(d, 0), OutputFile)
d = Dump(lzw(d, 1), OutputFile)
End If
Return c
End Function
End Module
Also what do
a, d, j, and pd (all ints) refer to in your original code?