Getting 2 ints in one STRING

Posted By: Razoron

Getting 2 ints in one STRING - 09/24/10 16:31

Hi,
to decrease bandwidth, I want to put 2 integers in one STRING.
How i can save them in one string without addition information? Just that STRING.
Greets Raz
Posted By: wdlmaster

Re: Getting 2 ints in one STRING - 09/24/10 16:50

Code:
str_cpy (s,"");
str_cat (s,format,int1);
str_cat (s,format,int2);


BUT: you can't really reduce bandwidth this way! Only with very small numbers. Example:
A 32bit integer is 4 byte, no matter how big the number is (up to it's maximum) - but if you represent high values as string and not as binary, it needs way more bytes. For example the value 2147483647 needs already 10 byte (instead of 4)!

With 10 bytes (binary), you could store two long and one short integer(s), or 5 short integers etc...
Posted By: Razoron

Re: Getting 2 ints in one STRING - 09/24/10 18:12

Yes, but I don't want to send them in a row.
@ your code:
And how to extract them?
Code:
function send(int i1, int i2)
{
	STRING* s;
	str_cpy(s, "");
	str_cat(s, i1);
	str_cat(s, i2);
	send(s);
}
function recieve(STRING* s)
{
	int i1;
	int i2;
	
	i1 = ??;
	i2 = ??;
}


Posted By: WretchedSid

Re: Getting 2 ints in one STRING - 09/24/10 18:14

1) Add a seperating character like !
2) Split the string into the two substrings seperated by the character.
3) Convert both into an integer.
4) ???
5) Profit
Posted By: wdlmaster

Re: Getting 2 ints in one STRING - 09/24/10 18:20

You must use a seperator for the two numbers. Then, use str_stri to get the position of the seperator and a combination of str_clip and str_trunc to "extract" the two values...
Posted By: MrGuest

Re: Getting 2 ints in one STRING - 09/25/10 16:10

Originally Posted By: wdlmaster
You must use a seperator for the two numbers. Then, use str_stri to get the position of the seperator and a combination of str_clip and str_trunc to "extract" the two values...
You don't always need a seperator if you know the length of the integer you're sending

If you're always sending the same length integer, just use str_clip() and str_trunc() to and extract the length you need

If you're changing the length of the integer, just concatenate that into the string
© 2024 lite-C Forums