Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
hello everyone
I've been trying to come up with a way to condense (compress) a large string that only contains number
is it possible to combining each couple of numbers and represent them in 8 bits instead of having each character take up the entire octet


Here is my input sample:

"543216548798654321320000654897986321"

each two numbers should take up 8 bits

thank you
Posted
Updated 25-Apr-20 5:38am
Comments
Sergey Alexandrovich Kryukov 4-Jan-16 16:10pm    
Of course, this is fairly simple. What have you tried so far?
What you suggested is the simplest, but not the best compression: 8 bits can store 256 numbers, but you want to store 100 (0-99).
—SA
Matt T Heffron 4-Jan-16 17:05pm    
"... 16 8 bits can store 256 numbers..."
FTFY ;-)
Sergey Alexandrovich Kryukov 4-Jan-16 17:21pm    
8, 8... :-)
—SA
Patrice T 4-Jan-16 18:15pm    
What have you done so far ?
Even number of figures is rather easy to handle. but odd number of figures is more tricky.
PIEBALDconsult 4-Jan-16 19:12pm    
Yes.

if you are REALLY SURE, that it will only be decimal numbers you can go as easy as this:
C++
char digit1 = sample[i];
int value1 = digit1 - '0'; //is between 0 and 9

char digit0 = sample[i+1];
int value0 = digit0 - '0'; //is between 0 and 9
int number = value1 * 10 + value0;

byteBuffer[n] = (byte) number;

The value number is ensured to be below 128 (7 bits), so you can spare out 1 bit and save the value in 7 bits. But that is advanced stuff and you should understand it like here from Microsoft fine explained.
 
Share this answer
 
Comments
CPallini 5-Jan-16 5:51am    
5.
C++
unsigned int i;
	const char *s = "123465789";
	vector<char> num;
	for (i=0;i<strlen(s);i+=2)
	{
		num.push_back((s[i]-'0') + (s[i+1]-'0')*10);
	}

would something like this work ?
will the size of the vector num be half the size of the string s ??
 
Share this answer
 
v4
Comments
Matt T Heffron 5-Jan-16 17:05pm    
As written it will omit the last digit of an odd length string. Also, be careful how this sequence of "char"s is used. If handed to any functions expecting a string, the occurrence of a 0 value could cause it to appear truncated. Perhaps you should "OR" in 0x80 to the value so it cannot be 0.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900