From one string to several integers
#1

Hello there

I'm making a tiki system (like horse shoes) where a player has to find 25 tikis that are hidden in the server.
One of the most important things about this is that even when a player has disconnected their tikis (the ones they've already found) remain 'found'.
I did this by making a 25 character string, where each character (either 0 or 1) represents each tiki.
So if a player has found tiki id 1, 2, 7 and 25 this is the string:
Код:
1100001000000000000000001
I then save this string in a database (so I do not have to make a new cel for every single tiki)
Now the saving works, when a player disconnects the string is properly saved, and when a player connects the string is properly loaded.
However I have some trouble turning the string (which now consists of characters 0 and 1) into integer values for each tiki

So this:
pawn Код:
1100001000000000000000001
Should become this:
pawn Код:
PInfo[playerid][FoundTiki][0] = 1; //Where 0 stands for the tiki id
PInfo[playerid][FoundTiki][1] = 1; //Where 1 stands for the tiki id
PInfo[playerid][FoundTiki][2] = 0; //Where 2 stands for the tiki id
I tried doing this with the following code:
pawn Код:
for(new ti; ti < sizeof TIInfo; ti ++) //TIInfo is an array that contains information for all tikis, so its size equals the total amount of tikis in the server
                {
                    PInfo[playerid][FoundTiki][ti] = strval(PInfo[playerid][TikiString][ti]); //convert each char into an int
                }
So what I try to do here is I start a loop goin from 0 to 25 and I then store the value of the string for a specific tikiid into the corresponding tiki.
But this does not seem to work :/

Does anyone know how I can make this work properly?
Any help is well appreciated
Thanks in advance
Reply
#2

use sscanf and delimiters.
Reply
#3

Quote:
Originally Posted by fordawinzz
Посмотреть сообщение
use sscanf and delimiters.
And how should I use sscanf to reverse formatting?
Reply
#4

I think this might work.
pawn Код:
for(new ti; ti < sizeof TIInfo; ti ++) //TIInfo is an array that contains information for all tikis, so its size equals the total amount of tikis in the server
{
    PInfo[playerid][FoundTiki][ti] = PInfo[playerid][TikiString][ti];
}
Reply
#5

Quote:
Originally Posted by knackworst
Посмотреть сообщение
And how should I use sscanf to reverse formatting?
pawn Код:
sscanf(PInfo[playerid][TikiString], "p<,>a<i>[25]", PInfo[playerid][FoundTiki]);
Reply
#6

Well I have no clue why you use a string for that if you just could put that in one 32 bit cell (a normal variable in pawn) with bit manipulation

To convert a number from a string you need to subtract the value of '0'
pawn Код:
PInfo[playerid][FoundTiki][ti] = PInfo[playerid][TikiString][ti] - '0';
But if you only use 1 or 0 than you could use a check
pawn Код:
PInfo[playerid][FoundTiki][ti] = (PInfo[playerid][TikiString][ti] == '1');
Reply
#7

Quote:
Originally Posted by Nero_3D
Посмотреть сообщение
Well I have no clue why you use a string for that if you just could put that in one 32 bit cell (a normal variable in pawn) with bit manipulation

To convert a number from a string you need to subtract the value of '0'
pawn Код:
PInfo[playerid][FoundTiki][ti] = PInfo[playerid][TikiString][ti] - '0';
But if you only use 1 or 0 than you could use a check
pawn Код:
PInfo[playerid][FoundTiki][ti] = (PInfo[playerid][TikiString][ti] == '1');
Thank you very much, this works
Btw what do you mean with store it into a 32-bit cel?
And how would I convert a string into different integers if there were more values than just 0 and 1, let's say 1, 2, 3?

(+Rep btw)
Reply
#8

Quote:
Originally Posted by knackworst
Посмотреть сообщение
Thank you very much, this works
Btw what do you mean with store it into a 32-bit cel?
And how would I convert a string into different integers if there were more values than just 0 and 1, let's say 1, 2, 3?

(+Rep btw)
Well you only use 0 or 1 also binary values than you can store these number binary

Lets recreate this number 1100001000000000000000001
pawn Код:
new variable = 0;

variable |= 1 << 0; // it starts from the right with 0
variable |= 1 << 18;
variable |= 1 << 23;
variable |= 1 << 24;

printf("0b%b", variable); // prints 0b1100001000000000000000001
There are enough bit manipulations tutorials out there

If you just want to convert a number within string to an integer than subtract the value of the character '0' fromt it
It should be clear if you look at the ASCII table ('0' represents 48 or 0x30)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)