Simon said:
Since my data is in byte, my values are in the range of 0-255. By
converting my stream of data to string (also tried based64String)
will totally messed up my data.
What I've tried was convert my byte() to string (using
Convert.ToBased64String) then find the delimiter position and extract
out the data portion, then convert the data portion back to byte().
It just messed up my compressed data.
No! No! Change it to use bytes. I used strings for my convenience, but as
I've been waiting for my computer to process a load of stuff I've had a few
minutes to have a go myself.
* If this is homework then do not read on until you have failed to change it
yourself. *
If you have a form with a textbox named TextBox1, then...
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim streemB As New ArrayList ' the pretend data
Dim bytes As String() =
Split("12,13,14,15,0,255,19,127,13,10,255,18,17,34,65,76,13,10,255,7,6,5,13,10,255,1,2,3",
",")
For i As Integer = LBound(bytes) To UBound(bytes)
streemB.Add(CByte(CInt(bytes(i))))
Next
Dim eop As New ArrayList ' the end-of-packet (eop) marker
eop.Add(13)
eop.Add(10)
eop.Add(255)
Dim packet As New ArrayList
Dim packetNo As Integer = 0
Dim eopPos As Integer = 0 ' position in eop
Dim streemPos As Integer = 0 ' position in streemB
Dim dataStart As Integer = 0
Dim dataEnd As Integer ' the end of a packet
While streemPos < streemB.Count
If CByte(streemB(streemPos)) = CByte(eop(eopPos)) Then
eopPos += 1
If eopPos = eop.Count Then
' we have found the end of a packet
' so copy it to its own ArrayList
packet.Add(New ArrayList)
dataEnd = streemPos - eop.Count
For i As Integer = dataStart To dataEnd
DirectCast(packet.Item(packetNo), ArrayList).Add(streemB(i))
Next
packetNo += 1
dataStart = streemPos + 1
eopPos = 0
End If
Else
' this byte was not part of the eop marker, therefore not at end of
packet
eopPos = 0
End If
streemPos += 1
End While
printit:
Dim s As New System.Text.StringBuilder
Dim a As ArrayList
For i As Integer = 0 To packet.Count - 1
a = DirectCast(packet.Item(i), ArrayList)
For j As Integer = 0 To a.Count - 1
s.Append(a.Item(j).ToString)
s.Append(", ")
Next
' remove extra ", "
s.Length = Math.Max(s.Length - 2, 0)
s.Append(Environment.NewLine)
Next
' remove extra Environment.NewLine
s.Length = Math.Max(s.Length - Environment.NewLine.Length, 0)
TextBox1.Text = s.ToString
End Sub
(watch for line-
wrap)
The displayed output will be:
12, 13, 14, 15, 0, 255, 19, 127
18, 17, 34, 65, 76
7, 6, 5
- notice it hasn't included the last bytes (1,2,3) because it hasn't seen an
end-of-packet marker.
Additionally, I don't see much point in storing the bytes in an ArrayList if
it's a stream from a network. It will not be much effort for you to change
the code to work with a stream of unknown length.
The 2.0 .NET framework may make it tidier than having to use DirectCast to
tell it the ArrayList you put in is an ArrayList (grrrr...).
Um, how do you propose to ensure your compressed data does not just happen
to have the same sequence of bytes as the eop marker? Why not send the
length of the compressed data, followed by the compressed data?
Now, who's going to show us how to do it in three lines?
Andrew