I have this class to encode and decode a serialized class to a string and back.
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
Imports System.Text
Public Class FormData
Public Function Encode(fdi As FormDataItem) As String
Dim formatter As New BinaryFormatter
Dim ms As New MemoryStream
formatter.Serialize(ms, fdi)
ms.Position = 0
Dim output As New StringBuilder
Dim waarde As String = ""
Do While ms.Position < ms.Length
waarde = Hex(ms.ReadByte)
If waarde.Length = 1 Then waarde = "0" + waarde
output.Append(waarde)
Loop
Return output.ToString
End Function
Public Function Decode(text As String) As FormDataItem
Dim ms As New MemoryStream()
Dim waarde As Byte
Dim getal As String
For ab = 0 To text.Length - 1 Step 2
getal = text.Substring(ab, 2)
waarde = CByte(Val("&h" + getal))
ms.WriteByte(waarde)
Next
ms.Position = 0
Dim formatter As New BinaryFormatter
Dim fdi As New FormDataItem
ms.Position = 0
Try
fdi = DirectCast(formatter.Deserialize(ms), FormDataItem)
Catch ex As Exception
Return Nothing
End Try
Return fdi
End Function
End Class
Now I would like to compress the string in the encode function and decompress in the decode function because this data is used in URLs.
I'm a bit stuck on where and how to compress the data. Do I do that right after serialization or compress just the string?
any help is appreciated!
rg. Eric