SA-MP Forums Archive
Letters converted into numbers. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Letters converted into numbers. (/showthread.php?tid=200708)



Letters converted into numbers. - Giacomand - 19.12.2010

I'm trying to make a script where each player has a Bank Account Number. I want to be able to create this number from the player's name. Every A would be a 1 for example.

Is there an easy way without having to detect every letter inside of a very large switch operator? Thanks.


Re: Letters converted into numbers. - Rachael - 19.12.2010

Every character is an ASCII number, you don't need a switch statement to get it.
pawn Код:
for(new i; i < strlen(string);i++)
{
     printf("%c = %d",string[i],string[i]);
}
Then you just have to create a formula to create a large number from these small numbers ( easier said than done )

If you want to create a unique random account number for each player, you can try this
pawn Код:
stock returnnewcarid()
{
    new carfile[9],carstring[128];
    for(new i = 0; i <= 8; i++)
    {
        carfile[i] = random(9)+1;
    }
    format(carstring,sizeof(carstring), "%s%d%d%d%d%d%d%d%d%d.ini",CARS_PATH,carfile[0],carfile[1],carfile[2],carfile[3],carfile[4],carfile[5],carfile[6],carfile[7],carfile[8]);
    while(fexist(carstring))
    {
        for(new i = 0; i <= 8; i++)
        {
            carfile[i] = random(9)+1;
        }
        format(carstring,sizeof(carstring), "%s%d%d%d%d%d%d%d%d%d.ini",CARS_PATH,carfile[0],carfile[1],carfile[2],carfile[3],carfile[4],carfile[5],carfile[6],carfile[7],carfile[8]);
    }
    new regstring[12],new_code;
    format(regstring,sizeof(regstring),"%d%d%d%d%d%d%d%d%d",carfile[0],carfile[1],carfile[2],carfile[3],carfile[4],carfile[5],carfile[6],carfile[7],carfile[8]);
    new_code = strval(regstring);
    return new_code;
}
Its written for a car registration system, but you can modify it for your purposes. Just change CAR_PATH to a directory in scriptfiles, and create a new file there for every account number you create ( this will ensure each account number is unique )

I know it seems like a very long way to do this, but I wanted to create a unique 9 digit number with no zeros in it, and thats how I did it. Now that I look at it, there are better ways to do this, but :effort:

[edit] You could also use AUTO_INCREMENT in an sql table


Re: Letters converted into numbers. - Giacomand - 19.12.2010

Is there a way to get make it go back and fourth with ease? Like I can get the player name from the numbers?


Re: Letters converted into numbers. - Rachael - 19.12.2010

Not directly ( with the suggestion I made) , although you could put the players name in the .ini file that you create.

Otherwise the SQL solution, or even the hashing idea would give you this option.