473,386 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Compress a string

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

AAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5vdCB iZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAABQmnOxFPsd4 hT7HeIU+x3iEfdA4hb7HeIR9xLiFfsd4hH3QuIb+x3iB/NA4hb7HeKX80DiHfsd4hT7HOJY+x3iEfd94hD7HeIR90fiFfsd 4lJpY2gU+x3iAAAAAAAAAAAAAAAAAAAAAFBFAABMAQMAeaGCQQ AAAAAAAAAA4AAPAQsBBwoAEAAAABIAAAAAAADjGgAAABAAAAAg AAAAAEAAABAAAAACAAAEAAAAAAAAAAQAAAAAAAAAAEAAAAAEAA DXjAAAAwAAAAAAEAAAEAAAAAAQAAAQAAAAAAAAEAAAAAAAAAAA AAAAlCMAAIwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAEAhAAAcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 4CIAAEgAAAAAAAAAAAAAAAAgAABAAQAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAALnRleHQAAAA0DgAAABAAAAAQAAAABAAAAAAA AAAAAAAAAAAAIAAAYC5yZGF0YQAAtAoAAAAgAAAADAAAABQAAA AAAAAAAAAAAAAAAEAAAEAuZGF0YQAAAEQFAAAAMAAAAAIAAAAg AAAAAAAAAAAAAAAAAABAAADAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAA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JFRX/xWIIEAAg8QUZscEdwAA6yWLTCRAV1H/1Yt8JEyDxAiF23QNjVQkFFJX/9WDxAjrBWbHBwAAi1wkSIXbD4SoAAAAaHAhQABoXCFAAP8VbCB AAFD/FXAgQABmxwMAAGaDPwCL8A+EgAAAAIX2dEKNRCQQUGgAAAEAag BqAFdqAP/WhcB0F41MJBBRaAAAAgBqAGoAV2oA/9aFwHVOi1QkEIsCUFP/1YtMJBiDxAhR6zVoIDBAAFdqAOg3BQAAhcB0EWggMEAAV2oA6C AFAACFwHUYixUgMEAAUlP/1aEgMEAAg8QIUOj+BAAAi0wkNF9eXTPAW+hUCAAAg8Qow8zMzM zMzMzMzMzMg+wYVsdEJAQEAAAA/xU0IUAAi/CF9nUFXoPEGMNX/xVkIEAAUP8VMCFAAIv4hf90SmoBjUQkEFD/FRwgQABqAGoAagGNTCQYUf8VICBAAI1UJAxSjUQkDFBWizU4IU AA/9aFwHUd/xVoIEAAUGiwIUAA/xWUIEAAg8QIXzPAXoPEGMONTCQMUY1UJAxSV//WhcB1Hf8VaCBAAFBogCFAAP8VlCBAAIPECF8zwF6DxBjDX7gBA AAAXoPEGMPMzMzMg+wYU1ZXavb/FVwgQACLNWAgQACL+LsBAAAA6wONSQCNRCQMUFONTCQYUVf/1oXAdO6LRCQMhcB25mY5XCQQdd+LRCQUhcB012aLRCQeZoXAdM 1fXluDxBjDzMzMzMzMUVNVVldq9v8VXCBAAItMJBiL6KGkIE

Jun 19 '06 #1
14 3603
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.

Jun 19 '06 #2
Maybe something like RLE compression?
Meelis
"Hugh Janus" <my*************@hotmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
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

AAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5vdCB iZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAABQmnOxFPsd4 hT7HeIU+x3iEfdA4hb7HeIR9xLiFfsd4hH3QuIb+x3iB/NA4hb7HeKX80DiHfsd4hT7HOJY+x3iEfd94hD7HeIR90fiFfsd 4lJpY2gU+x3iAAAAAAAAAAAAAAAAAAAAAFBFAABMAQMAeaGCQQ AAAAAAAAAA4AAPAQsBBwoAEAAAABIAAAAAAADjGgAAABAAAAAg AAAAAEAAABAAAAACAAAEAAAAAAAAAAQAAAAAAAAAAEAAAAAEAA DXjAAAAwAAAAAAEAAAEAAAAAAQAAAQAAAAAAAAEAAAAAAAAAAA AAAAlCMAAIwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAEAhAAAcAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 4CIAAEgAAAAAAAAAAAAAAAAgAABAAQAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAALnRleHQAAAA0DgAAABAAAAAQAAAABAAAAAAA AAAAAAAAAAAAIAAAYC5yZGF0YQAAtAoAAAAgAAAADAAAABQAAA AAAAAAAAAAAAAAAEAAAEAuZGF0YQAAAEQFAAAAMAAAAAIAAAAg AAAAAAAAAAAAAAAAAABAAADAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AhR6zVoIDBAAFdqAOg3BQAAhcB0EWggMEAAV2oA6C AFAACFwHUYixUgMEAAUlP/1aEgMEAAg8QIUOj+BAAAi0wkNF9eXTPAW+hUCAAAg8Qow8zMzM zMzMzMzMzMg+wYVsdEJAQEAAAA/xU0IUAAi/CF9nUFXoPEGMNX/xVkIEAAUP8VMCFAAIv4hf90SmoBjUQkEFD/FRwgQABqAGoAagGNTCQYUf8VICBAAI1UJAxSjUQkDFBWizU4IU AA/9aFwHUd/xVoIEAAUGiwIUAA/xWUIEAAg8QIXzPAXoPEGMONTCQMUY1UJAxSV//WhcB1Hf8VaCBAAFBogCFAAP8VlCBAAIPECF8zwF6DxBjDX7gBA AAAXoPEGMPMzMzMg+wYU1ZXavb/FVwgQACLNWAgQACL+LsBAAAA6wONSQCNRCQMUFONTCQYUVf/1oXAdO6LRCQMhcB25mY5XCQQdd+LRCQUhcB012aLRCQeZoXAdM 1fXluDxBjDzMzMzMzMUVNVVldq9v8VXCBAAItMJBiL6KGkIE

Jun 19 '06 #3

Meelis Lilbok wrote:
Maybe something like RLE compression?


errrr.... sounds good. but no idea what it is! Could you expand on
that?
Hugh

Jun 19 '06 #4

http://www.prepressure.com/techno/compressionrle.htm
I try to find a class writen by me, i used it befgore for something :))
Meelis

"Hugh Janus" <my*************@hotmail.com> wrote in message
news:11*********************@f6g2000cwb.googlegrou ps.com...

Meelis Lilbok wrote:
Maybe something like RLE compression?


errrr.... sounds good. but no idea what it is! Could you expand on
that?
Hugh

Jun 19 '06 #5

Meelis Lilbok wrote:
http://www.prepressure.com/techno/compressionrle.htm
I try to find a class writen by me, i used it befgore for something :))
Meelis


Thanks Meelis, I have a feeling that that is what the code in the link
i posted does, or so it looks to me. What do you think? Either ways,
I would be interested in trying your class to see which one works
better!

Jun 19 '06 #6
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

"Hugh Janus" <my*************@hotmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...

Meelis Lilbok wrote:
http://www.prepressure.com/techno/compressionrle.htm
I try to find a class writen by me, i used it befgore for something :))
Meelis


Thanks Meelis, I have a feeling that that is what the code in the link
i posted does, or so it looks to me. What do you think? Either ways,
I would be interested in trying your class to see which one works
better!



Jun 19 '06 #7
ooops sry, there was a bug in my first code

rle2.zip is correc one :)

Meelis

"Meelis Lilbok" <me***********@deltmar.ee> wrote in message
news:ei**************@TK2MSFTNGP05.phx.gbl...
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

"Hugh Janus" <my*************@hotmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...

Meelis Lilbok wrote:
http://www.prepressure.com/techno/compressionrle.htm
I try to find a class writen by me, i used it befgore for something :))
Meelis


Thanks Meelis, I have a feeling that that is what the code in the link
i posted does, or so it looks to me. What do you think? Either ways,
I would be interested in trying your class to see which one works
better!




Jun 19 '06 #8

Meelis Lilbok wrote:
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

Jun 19 '06 #9
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
Jun 19 '06 #10
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



"Hugh Janus" <my*************@hotmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...

Meelis Lilbok wrote:
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

Jun 19 '06 #11

Meelis Lilbok wrote:
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?

Jun 19 '06 #12
One solution is to use 255 length blocks :)
if count is 255 then counting starts again from 1
Meelis


"Hugh Janus" <my*************@hotmail.com> wrote in message
news:11*********************@c74g2000cwc.googlegro ups.com...

Meelis Lilbok wrote:
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?
Jun 19 '06 #13
Do you have to write this code? or would it be acceptable to use a free
third-party open source .net library?

http://www.icsharpcode.com/OpenSourc...b/Default.aspx

I have used this library and whilst I can't give yuo any stats on speed
or compression I can at least say that it hasn't ever crashed ;-)

Jun 20 '06 #14


Meelis, your function works very well for me. Could you please post
the Uncompress function as well?

Jun 27 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: José Carlos | last post by:
Hi. How i could compress string of data?. I heart that it´s possible to make with librep, but i´dont know the way to do it. if somebody know any way to do it or any web where explain it i...
0
by: Patrick Questembert | last post by:
I am developping with Visual Studio 2003 + C# + MySQL 4.1 and the OleDb components. My problem is that a stament using the COMPRESS() function seems to work or not depending on the data ... Here...
3
by: Edward | last post by:
Thanks to Bob Powel (http://www.bobpowell.net/onebit.htm) I'm able to compress an image. However, this method takes a long time to compress an image. Does anyone know a quicker way? (The images I...
3
by: delphiconsultingguy | last post by:
Anybody? thanks, Sean
6
by: Champika Nirosh | last post by:
Hi, I have two machine where I needed to have a extended TCP/IP protocol to make the link between the two machines Mean,I need to write a application that compress every data the machine send...
6
by: Adriano | last post by:
Can anyone recommend a simple way to compress/decomress a String in .NET 1.1 ? I have a random string of 70 characters, the output from a DES3 encryption, and I wish to reduce the lengh of it, ...
0
by: vampire1986 | last post by:
Hi all. I have project compress file using C#. I'm using name space IO.Compression and i saw this code on Internet but it is compress to file bigger than source file. Can you help me, please.Thanks...
0
by: J. Clifford Dyer | last post by:
On Sun, May 18, 2008 at 07:06:10PM +0100, Matt Porter wrote regarding Compress a string: def compress_str(s): # str is a builtin keyword. Don't overload it. out = for c in s: if out and c ==...
0
by: Matt Porter | last post by:
On Sun, 18 May 2008 19:13:57 +0100, J. Clifford Dyer <jcd@sdf.lonestar.orgwrote: Thanks. Had to change a few bits to make it behave as I expected: def compress_str(s): # str is a builtin...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.