Another idea: you could use "run-length encoding". Wikipedia will be able to tell you about it. That's no good unless you array has runs of elements which are the same value, though.
The way you have it set up, you'd probably have to create some sort of data structure, with a few bits to indicate whether the next field was an absolute value, or a char offset from the previous value, or a short offset. To be honest, it's a *lot* more trouble than it's worth. You're using, what, 10*4 = 40 bytes of memory? Who cares?
If you have a lot of data that needs to be compressed, I'd use an external compression program for that (or you can make your own Huffman encoding or something, but be warned, it's far from easy). If you have a lot of data that won't all fit into memory, store most of it on disk and only load some of it at a time into memory. But compression schemes such as what you're trying to create probably won't be very useful . . . .
By the way, this
Code:
bool IsOneByteValue(int iValue)
{
if (iValue < 0)
{
if (iValue >= -128)
return true;
}
else
{
if (iValue <= 127)
return true;
}
return false;
}
can be written much more concisely as
Code:
bool IsOneByteValue(int iValue)
{
if (iValue >= -128 && iValue <= 127)
return true;
return false;
}
or even
Code:
bool IsOneByteValue(int iValue)
{
return (iValue >= -128 && iValue <= 127);
}