【发布时间】:2014-01-13 17:50:51
【问题描述】:
我想将代码从 vb6 迁移到 vb.net。 该项目非常基础(不使用 dll 或组件引用)只是模块
在压缩功能中,已经成功。我很开心。但是当解压子不成功。该程序被挂起。我尝试修复它,我发现问题是 do.. exit do..loop 语句。
注意:代码在vb6中运行成功。
这是我的解压代码
Public Sub Decompress(ByteArray() As Byte)
Dim InpPos As Long
Dim InBitPos As Integer
Dim LowValue As Long
Dim HighValue As Long
Dim RangValue As Long
Dim MidValue As Long
Dim Value As Long
Dim mChar As Byte
Dim i As Integer
Dim Index As Integer
Dim EOF_State As Boolean
Dim TopBit As Long
Dim One(256) As Long
Dim Zero(256) As Long
Call Initiate
LowValue = 0
HighValue = (2 ^ MaxBits) - 1
TopBit = 2 ^ (MaxBits - 1)
InpPos = 0
Value = ReadBitsFromArray(ByteArray, InpPos, InBitPos, MaxBits)
Index = -1
For i = 0 To 256
One(i) = 1
Zero(i) = 1
Next
Do
mChar = 0
For i = 0 To 7
Index = (1 * (2 ^ i)) - 1 + mChar
RangValue = HighValue - LowValue
MidValue = LowValue + (RangValue * (Zero(Index) / (One(Index) + Zero(Index))))
If MidValue = LowValue Then MidValue = MidValue + 1
If MidValue = HighValue - 1 Then MidValue = MidValue - 1
If Value >= MidValue Then
mChar = (2 * mChar) + 1
LowValue = MidValue
One(Index) = One(Index) + 1
Else
mChar = 2 * mChar
HighValue = MidValue
Zero(Index) = Zero(Index) + 1
End If
Do While (HighValue And TopBit) = (LowValue And TopBit) Or LowValue > HighValue - 255
If InpPos <= UBound(ByteArray) Then
Value = (Value And (TopBit - 1)) * 2 + ReadBitsFromArray(ByteArray, InpPos, InBitPos, 1)
HighValue = (HighValue And (TopBit - 1)) * 2 + 1
LowValue = (LowValue And (TopBit - 1)) * 2
If LowValue >= HighValue Then HighValue = (2 ^ MaxBits) - 1
Else
EOF_State = True
Exit Do
End If
Loop
If EOF_State = True Then Exit Do
Next
Call AddmCharToArray(OutStream, OutPos, mChar)
Loop
ReDim Preserve OutStream(OutPos - 1)
End Sub
Private Sub Initiate()
ReDim OutStream(500)
OutPos = 0
OutBitCount = 0
OutByteBuf = 0
End Sub
Private Sub AddBitsToOutStream(Number As Integer)
OutByteBuf = OutByteBuf * 2 + Number
OutBitCount = OutBitCount + 1
If OutBitCount = 8 Then
OutStream(OutPos) = OutByteBuf
OutBitCount = 0
OutByteBuf = 0
OutPos = OutPos + 1
If OutPos > UBound(OutStream) Then
ReDim Preserve OutStream(OutPos + 500)
End If
End If
End Sub
Private Function ReadBitsFromArray(FromArray() As Byte, FromPos As Long, FromBit As Integer, NumBits As Integer) As Long
Dim i As Integer
Dim Temp As Long
For i = 1 To NumBits
Temp = Temp * 2 + (-1 * ((FromArray(FromPos) And 2 ^ (7 - FromBit)) > 0))
FromBit = FromBit + 1
If FromBit = 8 Then
If FromPos + 1 > UBound(FromArray) Then
Do While i < NumBits
Temp = Temp * 2
i = i + 1
Loop
FromPos = FromPos + 1
Exit For
End If
FromPos = FromPos + 1
FromBit = 0
End If
Next
ReadBitsFromArray = Temp
End Function
Private Sub AddmCharToArray(ToArray() As Byte, ToPos As Long, mChar As Byte)
If ToPos > UBound(ToArray) Then ReDim Preserve ToArray(ToPos + 500)
ToArray(ToPos) = mChar
ToPos = ToPos + 1
End Sub
做..退出做..
Do
mChar = 0
For i = 0 To 7
Index = (1 * (2 ^ i)) - 1 + mChar
RangValue = HighValue - LowValue
MidValue = LowValue + (RangValue * (Zero(Index) / (One(Index) + Zero(Index))))
If MidValue = LowValue Then MidValue = MidValue + 1
If MidValue = HighValue - 1 Then MidValue = MidValue - 1
If Value >= MidValue Then
mChar = (2 * mChar) + 1
LowValue = MidValue
One(Index) = One(Index) + 1
Else
mChar = 2 * mChar
HighValue = MidValue
Zero(Index) = Zero(Index) + 1
End If
Do While (HighValue And TopBit) = (LowValue And TopBit) Or LowValue > HighValue - 255
If InpPos <= UBound(ByteArray) Then
Value = (Value And (TopBit - 1)) * 2 + ReadBitsFromArray(ByteArray, InpPos, InBitPos, 1)
HighValue = (HighValue And (TopBit - 1)) * 2 + 1
LowValue = (LowValue And (TopBit - 1)) * 2
If LowValue >= HighValue Then HighValue = (2 ^ MaxBits) - 1
Else
EOF_State = True
Exit Do
End If
Loop
If EOF_State = True Then Exit Do
Next
Call AddmCharToArray(OutStream, OutPos, mChar)
Loop
对不起,我的英文写得不好。
【问题讨论】:
标签: vb.net vb6 vb6-migration