SA-MP Forums Archive
String manipulation - 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)
+--- Thread: String manipulation (/showthread.php?tid=441676)



String manipulation - dusk - 03.06.2013

Hello, i was doing something that doesn't matter and came across a need to convert a string to a integer and backwards, so i made myself these functions:
pawn Код:
stock StringToInt(str[])
{
    new tmp[5];
    format(tmp,sizeof(tmp),"%d",str);
    return strval(tmp);
}
stock IntToString(int)
{
    new tmp[5];
    format(tmp,sizeof(tmp),"%s",int);
    return tmp;
}
Then I have some letter and number combination, i extract the letter part. Then, I separate all three characters:
pawn Код:
new Char[2],Char2[2],Char3[2];
        strmid(Char,Raides,0,1);
        strmid(Char2,Raides,1,2);
        strmid(Char3,Raides,2,3);
        printf("Pirma:%s    Antra:%s        Trecia:%s",Char,Char2,Char3);//They are printed correctly.

Now the problematic part, I try to increase the int value of Char3 by one and convert it back to a letter:
pawn Код:
printf("StringToInt Char3:%d",StringToInt(Char3));
        new NewChar;
        NewChar = StringToInt(Char3);
        NewChar++;
        printf("NewChar:%s",IntToString(NewChar));
These are the two prints from above:
pawn Код:
[21:37:33] StringToInt Char3:67
[21:37:33] NewChar:DPDC
The number value of the letter "C" (which i use to test this) is 67 (I checked ASCII), then i add 1 to it, which is 68 and is the letter "D", so where did "DPDC" come from?

I do not see logic in this. Or am I making a mistake somewhere?


Re: String manipulation - [HiC]TheKiller - 04.06.2013

String -> Integer is just strval.
Integer -> String is either format or valstr.


Re: String manipulation - dusk - 04.06.2013

Ah, but the function "strval" would give me the intger IN the string, and i want the string itself as a number. The letters number from ASCII


Re: String manipulation - Vince - 04.06.2013

What? If you want to print a value as a char you use the %c placeholder in format.


Re: String manipulation - dusk - 04.06.2013

Thats okay for printing. But what if i want the ASCII value to be stored in an integer? Or im not getting something?


Re: String manipulation - dusk - 04.06.2013

****** saves the day once more! Why i always search for difficult sollotions to a simple problem..


Re: String manipulation - dusk - 04.06.2013

Sorry, it seems I still don't understand something:
pawn Код:
Char3 = Raides[2];
        Char2 = Raides[1];
        Char1 = Raides[0];
        Char3++;
        if(Char3 > 90)
        {
            Char2++;
            if(Char2 > 90) Char1++;
        }
        printf("Ints: C1:%d, C2:%d, C3:%d Strings:C1%s, C2%s, C3%s",Char1,Char2,Char3,Char1,Char2,Char3);
And it prints:
pawn Код:
[20:22:03] Ints: C1:65, C2:66, C3:68 Strings:C1A, C2BA, C3DBA
[20:22:03] Nenumber:ABADBA-



Re: String manipulation - MP2 - 04.06.2013

What on earth are you actually trying to do?


Re: String manipulation - dusk - 04.06.2013

I want to change a letter in to the next one. Like from A to B and so on...


Re: String manipulation - MP2 - 04.06.2013

Quote:
Originally Posted by dusk
Посмотреть сообщение
I want to change a letter in to the next one. Like from A to B and so on...
Not sure why on earth you'd want to do that - but okay:

pawn Код:
string[index]++;
There you go.