Compress a string

  • Thread starter Thread starter Hugh Janus
  • Start date Start date
H

Hugh Janus

Hi all,

I have several *long* strings (see bottom of post for an example) which
I will be sending across a network. Therefore, I want to compress them
for speed and because later they will be stored in a DB.
I found this code here http://abstractvb.com/code.asp?A=931 but it
doesn't seem to give me a particuarly good level of compression it
seems. As you can see in the article below, there is a lot of
repitition. So I was hoping that someone here could tell me that
either the code at the link is correct, what is wrong with it or of
another method I could use to compress my string(s).


TIA





AAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5vdCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAABQmnOxFPsd4hT7HeIU+x3iEfdA4hb7HeIR9xLiFfsd4hH3QuIb+x3iB/NA4hb7HeKX80DiHfsd4hT7HOJY+x3iEfd94hD7HeIR90fiFfsd4lJpY2gU+x3iAAAAAAAAAAAAAAAAAAAAAFBFAABMAQMAeaGCQQAAAAAAAAAA4AAPAQsBBwoAEAAAABIAAAAAAADjGgAAABAAAAAgAAAAAEAAABAAAAACAAAEAAAAAAAAAAQAAAAAAAAAAEAAAAAEAADXjAAAAwAAAAAAEAAAEAAAAAAQAAAQAAAAAAAAEAAAAAAAAAAAAAAAlCMAAIwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAhAAAcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA4CIAAEgAAAAAAAAAAAAAAAAgAABAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAALnRleHQAAAA0DgAAABAAAAAQAAAABAAAAAAAAAAAAAAAAAAAIAAAYC5yZGF0YQAAtAoAAAAgAAAADAAAABQAAAAAAAAAAAAAAAAAAEAAAEAuZGF0YQAAAEQFAAAAMAAAAAIAAAAgAAAAAAAAAAAAAAAAAABAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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QkFFJX/9WDxAjrBWbHBwAAi1wkSIXbD4SoAAAAaHAhQABoXCFAAP8VbCBAAFD/FXAgQABmxwMAAGaDPwCL8A+EgAAAAIX2dEKNRCQQUGgAAAEAagBqAFdqAP/WhcB0F41MJBBRaAAAAgBqAGoAV2oA/9aFwHVOi1QkEIsCUFP/1YtMJBiDxAhR6zVoIDBAAFdqAOg3BQAAhcB0EWggMEAAV2oA6CAFAACFwHUYixUgMEAAUlP/1aEgMEAAg8QIUOj+BAAAi0wkNF9eXTPAW+hUCAAAg8Qow8zMzMzMzMzMzMzMg+wYVsdEJAQEAAAA/xU0IUAAi/CF9nUFXoPEGMNX/xVkIEAAUP8VMCFAAIv4hf90SmoBjUQkEFD/FRwgQABqAGoAagGNTCQYUf8VICBAAI1UJAxSjUQkDFBWizU4IUAA/9aFwHUd/xVoIEAAUGiwIUAA/xWUIEAAg8QIXzPAXoPEGMONTCQMUY1UJAxSV//WhcB1Hf8VaCBAAFBogCFAAP8VlCBAAIPECF8zwF6DxBjDX7gBAAAAXoPEGMPMzMzMg+wYU1ZXavb/FVwgQACLNWAgQACL+LsBAAAA6wONSQCNRCQMUFONTCQYUVf/1oXAdO6LRCQMhcB25mY5XCQQdd+LRCQUhcB012aLRCQeZoXAdM1fXluDxBjDzMzMzMzMUVNVVldq9v8VXCBAAItMJBiL6KGkIE
 
Maybe something like RLE compression?


Meelis


Hugh Janus said:
Hi all,

I have several *long* strings (see bottom of post for an example) which
I will be sending across a network. Therefore, I want to compress them
for speed and because later they will be stored in a DB.
I found this code here http://abstractvb.com/code.asp?A=931 but it
doesn't seem to give me a particuarly good level of compression it
seems. As you can see in the article below, there is a lot of
repitition. So I was hoping that someone here could tell me that
either the code at the link is correct, what is wrong with it or of
another method I could use to compress my string(s).


TIA





AAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5vdCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAABQmnOxFPsd4hT7HeIU+x3iEfd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MCFAAIv4hf90SmoBjUQkEFD/FRwgQABqAGoAagGNTCQYUf8VICBAAI1UJAxSjUQkDFBWizU4IUAA/9aFwHUd/xVoIEAAUGiwIUAA/xWUIEAAg8QIXzPAXoPEGMONTCQMUY1UJAxSV//WhcB1Hf8VaCBAAFBogCFAAP8VlCBAAIPECF8zwF6DxBjDX7gBAAAAXoPEGMPMzMzMg+wYU1ZXavb/FVwgQACLNWAgQACL+LsBAAAA6wONSQCNRCQMUFONTCQYUVf/1oXAdO6LRCQMhcB25mY5XCQQdd+LRCQUhcB012aLRCQeZoXAdM1fXluDxBjDzMzMzMzMUVNVVldq9v8VXCBAAItMJBiL6KGkIE
 
p.s. all the other examples i find are for compressing files. I want
to compress a string in memory, send it and then discard.
 
Here, i found my class.

It works, when in your text are 4-255 repeated characters, if there are more
chars then 255 repated, you must modify my code

Works like this

a) Searches repated charactes and counts them
b) If character changes, saves control character, count of repated
charaters(as byte) and character to result string


Hope this helps a little bit ;)


meelis
 
Meelis said:
Here, i found my class.

It works, when in your text are 4-255 repeated characters, if there are more
chars then 255 repated, you must modify my code

Works like this

a) Searches repated charactes and counts them
b) If character changes, saves control character, count of repated
charaters(as byte) and character to result string


Hope this helps a little bit ;)

Thanks! But your file is not attached. Could you paste the class file
here as text?

Also, what if I do not know how many repeated characters there are in
my string? You say that yours works for 4-255 repeated characters, but
what if the number is unknown?

Hugh
 
Public Class clsCompress
Public Function CompressString(ByVal controlchar As String, ByVal text As
String) As String
Dim iPos As Integer, iLen As Integer, iCharCnt As Integer = 1, sChar As
String
Dim sResult As New System.Text.StringBuilder
'Get string length
iLen = text.Length
'Get first char
sChar = Left$(text, 1)
'Loop trough string
For iPos = 2 To iLen
'If char has changed
If sChar <> Mid$(text, iPos, 1) Then
'If CharCount is bigger then 3, add countrol char, count of chars as
Chrw(count) and char
If iCharCnt > 3 Then
sResult.Append(controlchar & ChrW(iCharCnt) & sChar)
iCharCnt = 1
Else
'Add just char
sResult.Append(sChar, iCharCnt)
iCharCnt = 1
End If
Else
'Get next char
sChar = Mid$(text, iPos, 1)
iCharCnt = iCharCnt + 1
End If
'Get next char
sChar = Mid$(text, iPos, 1)
Next
If iCharCnt > 3 Then
sResult.Append(controlchar & ChrW(iCharCnt) & sChar)
iCharCnt = 1
Else
'Add just char
sResult.Append(sChar, iCharCnt)
End If
Return sResult.ToString
End Function
End Class
 
This wersion works like this

lets say your string is abcdeeeeefg and control char is *


after compressing abcd*efg

now when there are more then 255 e-chars, my function does not work anymore.
then its must be modified to store length with 2 bytes


Sry, englis is not my native language ;)
Meelis
 
Meelis said:
This wersion works like this

lets say your string is abcdeeeeefg and control char is *


after compressing abcd*efg

now when there are more then 255 e-chars, my function does not work anymore.
then its must be modified to store length with 2 bytes


Sry, englis is not my native language ;)
Meelis
Ahhh, now I understand perfectly! thanks for your help. I think i'll
need to modify the code because I don't know if i will find more than
255 characters repeated, it is very possible. Do you know what changes
are required for your class to work with 2 bytes instead?
 
One solution is to use 255 length blocks :)
if count is 255 then counting starts again from 1


Meelis





Meelis said:
This wersion works like this

lets say your string is abcdeeeeefg and control char is *


after compressing abcd*efg

now when there are more then 255 e-chars, my function does not work
anymore.
then its must be modified to store length with 2 bytes


Sry, englis is not my native language ;)
Meelis
Ahhh, now I understand perfectly! thanks for your help. I think i'll
need to modify the code because I don't know if i will find more than
255 characters repeated, it is very possible. Do you know what changes
are required for your class to work with 2 bytes instead?
 
Meelis, your function works very well for me. Could you please post
the Uncompress function as well?
 
Back
Top