Hirvesh
Hirvesh

Reputation: 7974

String Compression Algorithm

I've been wanting to compress a string in C++ and display it's compressed state to console. I've been looking around for something and can't find anything appropriate so far. The closest thing I've come to finding it this one:

How to simply compress a C++ string with LZMA

However, I can't find the lzma.h header which works with it anywhere.

Basically, I am looking for a function like this:

std::string compressString(std::string uncompressedString){
//Compression Code

return compressedString;
}

The compression algorithm choice doesn't really matter. Anybody can help me out finding something like this? Thank you in advance! :)

Upvotes: 2

Views: 5810

Answers (1)

fvu
fvu

Reputation: 32953

Based on the pointers in the article I'm fairly certain they are using XZ Utils, so download that project and make the produced library available in your project.

However, two caveats:

  • dumping a compressed string to the console isn't very useful, as that string will contain all possible byte values, most of which aren't displayable on a console
  • compressing short strings (actually any small quantity of data) isn't what most general-purpose compressors were designed for, in many cases the compressed result will be as big or even bigger than the input. However, I have no experience with LZMA on small data quantities, an extensive test with data representative for your use case will tell you whether it works as expected.

One algorithm I've been playing with that gives good compression on small amounts of data (tested on data chunks sized 300-500 bytes) is range encoding.

Upvotes: 6

Related Questions