C/Compressing data without libraries
Expert: Narendra - 8/29/2006
QuestionHow would i go about compressing a struct-variable?
This is somewhat what it looks like:
[code]
struct tpmove {
int x, y, vx, vy;
char msg[300];
// There is more data, but you get the general picture
};
struct tpmove tvar;
// function defined elsewhere
send2server( (void *)&tvar );
[/code]
How would i compress 'tvar'?
When the data is sent, i should also note
I really want to avoid using external libraries.
My thoughts where to convert it to a (char *), then somehow compress, then send off, but it's the compression that is continually stumping me
thanks,
Alex~
AnswerCompression is not a simple thing.
It mainly depends on the type of data you are handling.
In your case, you are having text data.
So, it uses only 7 bits in a byte.
And you can use the 1 extra bit in the byte to store and this will give a compression of 12.5%.
But most of the compression algorithms work differently.
You have msg of 300 characters.
So, it will have many repeating characters.
And there are only 26 characters and you need only 5 bits to store all the possible characters.
But, some characters may mever come and some may come very frequently.
Or you can see everythings as 0 or 1 (bits).
So, you have chain of 0's and 1's.
If there are 10 0's, instead of writing '0' ten times, you can just write 0.10 (or some other notation that you like)
So, you will have can use this to your advantage.
Even though you don't want to use external library, you will still have to bank on a compression algorithm and this implementation has to be part of your code.
I think that is the only way, you can do this.