Useful Functions

strCopyEx - Copies a string to another string until it reaches the given delimiter:
pawn Код:
stock strCopyEx(szSrc[], szDest[], const szDelim[], iSize = sizeof(szDest)) {
    new
        iPos
    ;
    if((iPos = strfind(szSrc, szDelim[0], true)) != -1) {
        szSrc[iPos] = EOS;
        iSize = strcat(szDest, szSrc, iSize);
        szSrc[iPos] = szDelim[0];
    }
    else {
        strcat(szDest, szSrc, iSize);
    }
    return iSize;
}
Example(s):
pawn Код:
new
    szStr[] = "This-is-a-test-string-!",
    szDest[7]
;
strCopyEx(szStr[5], szDest, "-");
print(szDest); // Prints "is"

szDest[0] = EOS;

strCopyEx(szStr[15], szDest, "-");
print(szDest); // Prints "string"
Analog to this function:

strExplode - Splits a string in parts (like "explode" in PHP):
pawn Код:
stock strExplode(szStr[], const szDelim[], szOutput[][], const iSize = sizeof(szOutput)) {
    new
        i,
        iPos,
        iLen = strlen(szStr)
    ;
    do {
        iPos += strCopyEx(szStr[iPos], szOutput[i], szDelim[0], iLen) + 1;
    }
    while(iPos < iLen && ++i < iSize);
    return i;
}
Example(s):
pawn Код:
new
    szStr[] = "This-is-a-test-string-!",
    szOutput[6][7] // We have 6 string parts and the biggest length is 7
;
strExplode(szStr, "-", szOutput);

print(szOutput[0]); // This
print(szOutput[1]); // is
print(szOutput[2]); // a
print(szOutput[3]); // test
print(szOutput[4]); // string
print(szOutput[5]); // !
Reply

Quote:
Originally Posted by RyDeR`
Посмотреть сообщение
<...>
I'm still wondering why you write "sz" before every variable/array you make.

Nice functions, however.
Reply

Quote:
Originally Posted by Lorenc_
Посмотреть сообщение
I'm still wondering why you write "sz" before every variable/array you make.

Nice functions, however.
Hungarian notation. The variables are prefixed with their "type".

szTest[] = string, zero-terminated (all strings in PAWN are what's called zero-terminated, i.e. the text ends at the first 0 in that array).
fTest = float
iTest = integer


Also, RyDeR`, you should make sure the length of the 2nd dimension in the output for your explode function has boundary checks. Right now this will cause strange behavior:
pawn Код:
new
        szStr[] = "This-is-a-test-stringgggggg-!",
        szOutput[6][7] // We have 6 string parts and the biggest length is 7
    ;
    strExplode(szStr, "-", szOutput);
Reply

Quote:
Originally Posted by Slice
Посмотреть сообщение
Hungarian notation. The variables are prefixed with their "type".

szTest[] = string, zero-terminated (all strings in PAWN are what's called zero-terminated, i.e. the text ends at the first 0 in that array).
fTest = float
iTest = integer


Also, RyDeR`, you should make sure the length of the 2nd dimension in the output for your explode function has boundary checks. Right now this will cause strange behavior:
pawn Код:
new
        szStr[] = "This-is-a-test-stringgggggg-!",
        szOutput[6][7] // We have 6 string parts and the biggest length is 7
    ;
    strExplode(szStr, "-", szOutput);
You learn something new everyday!
Reply

Quote:
Originally Posted by Slice
Посмотреть сообщение
Hungarian notation. The variables are prefixed with their "type".

szTest[] = string, zero-terminated (all strings in PAWN are what's called zero-terminated, i.e. the text ends at the first 0 in that array).
fTest = float
iTest = integer
LMAO, I use it even on strings/floats/ints now since it does make code look cooler :O

pawn Код:
stock IsWeaponInAnySlot(playerid, weaponid)
{
    static
        szWeapon, szAmmo
    ;
   
    GetPlayerWeaponData(playerid, GetWeaponSlot(weaponid), szWeapon, szAmmo);
        if(szWeapon == weaponid) return true;

    #pragma unused szAmmo

    return false;
}

stock GetPlayerWeaponInSlot(playerid, slot)
{
    static
        szWeapon, szAmmo
    ;
   
    GetPlayerWeaponData(playerid, slot, szWeapon, szAmmo);
    #pragma unused szAmmo
   
    return szWeapon;
}

stock GetWeaponSlot(weaponid)
{
    switch(weaponid)
    {
        case WEAPON_BRASSKNUCKLE:
            return 0;
        case WEAPON_GOLFCLUB .. WEAPON_CHAINSAW:
            return 1;
        case WEAPON_COLT45 .. WEAPON_DEAGLE:
            return 2;
        case WEAPON_SHOTGUN .. WEAPON_SHOTGSPA:
            return 3;
        case WEAPON_UZI, WEAPON_MP5, WEAPON_TEC9:
            return 4;
        case WEAPON_AK47, WEAPON_M4:
            return 5;
        case WEAPON_RIFLE, WEAPON_SNIPER:
            return 6;
        case WEAPON_ROCKETLAUNCHER .. WEAPON_MINIGUN:
            return 7;
        case WEAPON_GRENADE .. WEAPON_MOLTOV, WEAPON_SATCHEL:
            return 8;
        case WEAPON_SPRAYCAN .. WEAPON_CAMERA:
            return 9;
        case WEAPON_DILDO .. WEAPON_FLOWER:
            return 10;
        case 44, 45, WEAPON_PARACHUTE:
            return 11;
        case WEAPON_BOMB:
            return 12;
    }
    return -1;
}
I written up some functions. I guess they will be a heckload handy. Especially to me.
Reply

Quote:
Originally Posted by Lorenc_
Посмотреть сообщение
LMAO, I use it even on strings/floats/ints now since it does make code look cooler :O

pawn Код:
stock IsWeaponInAnySlot(playerid, weaponid)
{
    static
        szWeapon, szAmmo
    ;
// .. moar code ..
I written up some functions. I guess they will be a heckload handy. Especially to me.
szAmmo and szWeapon is wrong! Those are plain integer variables! Should be iWeapon and iAmmo.
Reply

Some little functions I wrote up now, may be useful may not be useful

pawn Код:
#define setnull(%1)                 (%1[0]='\0') // similar to slices - Make a string "NULL"
#define inttobool(%1,%2)            (%2=!!%1) // INT to BOOL
#define Bit8:%1<%2>                 %1[(%2) char] // Creates a "CHAR" array, aka a 8bit array
#define Bit8_Get(%1,%2)             (%1{%2}) // Gets the "CHAR" array value (pretty self explanatory)
#define SetValue_2D(%1)             {_:%1,...} // Sets a value on a "2D" array

new
    Text: Hi[5] = SetValue_2D(INVALID_TEXT_DRAW),
    Bit8: Yo <5>
;

if(Bit8_Get(Yo, playerid) == 25) return true;
Reply

Quote:
Originally Posted by [HLF]Southclaw
Посмотреть сообщение
I found a little problem with strdel, it seems like it just sets the pos cell to '\0' and still leaves the rest.
Because I was writing a function and in it, it looped backwards through a string to find the last of something, and even though strdel had supposedly deleted from pos 40 it still found the character after this position.

I did a quick print test and deleted part of a string

str[20];
str = "a string that's 20.";
strdel(str, 10, 20);


Then printed the remaining part, that was supposed to be deleted:
print(str[11]);

"hat's 20."

So I made my own version that loops from the start to end and sets them all to 0.
Not sure if this is a function bug or I have a crappy version defined somewhere.
Strings are zero-terminated, meaning they're read until the first zero. Ex:
pawn Код:
new test[] = {'h', 'e', 'l', 'l', 'o', 0, 'w', 'o', 'r', 'l', 'd'};

print(test);
That would print "hello" because the string ends at the 0 after that.

Deleting after the 0 is pretty much useless and inefficient (unless you're planning to use the array for something other than strings).
Reply

RyDeR thank you very much for that function <3 <3
Reply

pawn Код:
stock SetVehicleNumberPlateEx(vehicleid, numberplate[])
{
    static
        iLoopIdx,
        iReturnValue,
        iDamageStatus[4],
        iComponents[14],
        Float: fPos[3],
        Float: fAngle,
        Float: fVelocity[3],
        Float: fHealth
    ;
   
    if ( !GetVehicleModel(vehicleid) )
        return 0;
   
    if ( strlen(numberplate) > 32 )
        numberplate[32] = 0;
       
    GetVehiclePos(vehicleid, fPos[0], fPos[1], fPos[2]);
    GetVehicleZAngle(vehicleid, fAngle);
    GetVehicleVelocity(vehicleid, fVelocity[0], fVelocity[1], fVelocity[2]);
    GetVehicleHealth(vehicleid, fHealth);
    GetVehicleDamageStatus(vehicleid, iDamageStatus[0], iDamageStatus[1], iDamageStatus[2], iDamageStatus[3]);
   
    for ( iLoopIdx = 0; iLoopIdx < 14; iLoopIdx++ )
    {
        iComponents[iLoopIdx] = GetVehicleComponentInSlot(vehicleid, iLoopIdx);
    }
   
    new bPlayerData[MAX_PLAYERS] = { -1, ... };
    for ( iLoopIdx = 0; iLoopIdx < 500; iLoopIdx++ )
    {
        if ( IsPlayerInVehicle(iLoopIdx, vehicleid) )
        {
            bPlayerData[iLoopIdx] = GetPlayerVehicleSeat(iLoopIdx);
            RemovePlayerFromVehicle(iLoopIdx);
            SetPlayerPos(iLoopIdx, fPos[0], fPos[1], fPos[2]); // if RPFV would not work
        }
    }
   
    iReturnValue = SetVehicleNumberPlate(vehicleid, numberplate);
    SetVehicleToRespawn(vehicleid);
   
    for ( iLoopIdx = 0; iLoopIdx < 500; iLoopIdx++ )
    {
        if ( bPlayerData[iLoopIdx] != -1 )
        {
            PutPlayerInVehicle(iLoopIdx, vehicleid, bPlayerData[iLoopIdx]);
        }
    }
   
    SetVehiclePos(vehicleid, fPos[0], fPos[1], fPos[2]);
    SetVehicleZAngle(vehicleid, fAngle);
    SetVehicleVelocity(vehicleid, fVelocity[0], fVelocity[1], fVelocity[2]);
    SetVehicleHealth(vehicleid, fHealth);
    UpdateVehicleDamageStatus(vehicleid, iDamageStatus[0], iDamageStatus[1], iDamageStatus[2], iDamageStatus[3]);
   
    for ( iLoopIdx = 0; iLoopIdx < 14; iLoopIdx++ )
    {
        if (iComponents[iLoopIdx] > 999) AddVehicleComponent(vehicleid, iComponents[iLoopIdx]);
    }
   
    return iReturnValue;
}
Reply

Quote:
Originally Posted by KoczkaHUN
Посмотреть сообщение
pawn Код:
stock SetVehicleNumberPlateEx(vehicleid, numberplate[])
{
    static
        iLoopIdx,
        iReturnValue,
        iDamageStatus[4],
        Float: fPos[3],
        Float: fAngle,
        Float: fVelocity[3],
        Float: fHealth
    ;
   
    if ( !GetVehicleModel(vehicleid) )
        return 0;
   
    if ( strlen(numberplate) > 32 )
        numberplate[32] = 0;
       
    GetVehiclePos(vehicleid, fPos[0], fPos[1], fPos[2]);
    GetVehicleZAngle(vehicleid, fAngle);
    GetVehicleVelocity(vehicleid, fVelocity[0], fVelocity[1], fVelocity[2]);
    GetVehicleHealth(vehicleid, fHealth);
    GetVehicleDamageStatus(vehicleid, iDamageStatus[0], iDamageStatus[1], iDamageStatus[2], iDamageStatus[3]);
   
    new bPlayerData[MAX_PLAYERS] = { -1, ... };
    for ( iLoopIdx = 0; iLoopIdx < 500; iLoopIdx++ )
    {
        if ( IsPlayerInVehicle(iLoopIdx, vehicleid) )
        {
            bPlayerData[iLoopIdx] = GetPlayerVehicleSeat(iLoopIdx);
            RemovePlayerFromVehicle(iLoopIdx);
            SetPlayerPos(iLoopIdx, fPos[0], fPos[1], fPos[2]); // if RPFV would not work
        }
    }
   
    iReturnValue = SetVehicleNumberPlate(vehicleid, numberplate);
    SetVehicleToRespawn(vehicleid);
   
    for ( iLoopIdx = 0; iLoopIdx < 500; iLoopIdx++ )
    {
        if ( bPlayerData[iLoopIdx] != -1 )
        {
            PutPlayerInVehicle(iLoopIdx, vehicleid, bPlayerData[iLoopIdx]);
        }
    }
   
    SetVehiclePos(vehicleid, fPos[0], fPos[1], fPos[2]);
    SetVehicleZAngle(vehicleid, fAngle);
    SetVehicleVelocity(vehicleid, fVelocity[0], fVelocity[1], fVelocity[2]);
    SetVehicleHealth(vehicleid, fHealth);
    UpdateVehicleDamageStatus(vehicleid, iDamageStatus[0], iDamageStatus[1], iDamageStatus[2], iDamageStatus[3]);
   
    return iReturnValue;
}
What about vehicle color & tuning parts? Virtual world? Interior?
Reply

Quote:
Originally Posted by wups
Посмотреть сообщение
What about vehicle color & tuning parts?
That won't disappear on SetVehicleToRespawn (altough color does not, this is 100%) - for the car parts, let me check it in a few minutes.

Edit: I fixed car parts in the code, thanks for suggestion.
Also I am experiencing a small bug: after respraying in a Transfender and then changing plates it will get the original color. Maybe just sync problem.
Reply

pawn Код:
#define Packed:%1<%2[%3]> %1[%2 char]=!(%3)

new
    Packed: Hey <25["Hi All"]>,
    Packed: Hey1 <25["Hi All"]>
;
Creates a packed string with a default value however, I am not sure how to make something like "packed_format" so it'll be compatible with these type of "packed" strings. Maybe I am wrong and "format" itself works on such things but yeah, small and easy macro. Yet useful.
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
How is:

pawn Код:
new
    Packed: Hey <25["Hi All"]>,
    Packed: Hey1 <25["Hi All"]>
;
Simpler than:

pawn Код:
new
    Hey[25 char] = !"Hi All",
    Hey1[25 char] = !"Hi All";
It's not even shorter.
Not really sure, however to me does look quite more simple (I don't know how but to me it does somehow).

Alright there we go, first un-needed macro created by Lorenc_ posted lol
Reply

Useful for debugging. Prints out a human-readable table of a database result.

Example (sorta):
pawn Код:
new DBResult:result = db_query(db, "SELECT * FROM blabla");

db_print_result(result);

db_free_result(result);
Beware that NULL values will cause it to crash, that's not my fault.

Example output:
As you can see some content is cut-off, it's because of the field length limit (which you can easily modify).
Код:
	/ type  | name            | tbl_name        | rootpage | sql                                            \
	|-------|-----------------|-----------------|----------|------------------------------------------------|
	| table | lw_lvdm_users   | lw_lvdm_users   | 2        | CREATE TABLE lw_lvdm_users(                    |
	|       |                 |                 |          |     userid integer primary key,                |
	|       |                 |                 |          |     name text,                                 |
	|       |                 |                 |          |     password                                   |
	|-------|-----------------|-----------------|----------|------------------------------------------------|
	| table | lw_lvdm_bans    | lw_lvdm_bans    | 4        | CREATE TABLE lw_lvdm_bans(                     |
	|       |                 |                 |          |     banid integer primary key,                 |
	|       |                 |                 |          |     name text,                                 |
	|       |                 |                 |          |     reporter t                                 |
	|-------|-----------------|-----------------|----------|------------------------------------------------|
	| index | bans_type       | lw_lvdm_bans    | 36886    | CREATE INDEX "bans_type" ON lw_lvdm_bans(type) |
	|-------|-----------------|-----------------|----------|------------------------------------------------|
	| index | bans_time       | lw_lvdm_bans    | 40587    | CREATE INDEX "bans_time" ON lw_lvdm_bans(time) |
	|-------|-----------------|-----------------|----------|------------------------------------------------|
	| table | lw_lvdm_ip_bans | lw_lvdm_ip_bans | 46040    | CREATE TABLE `lw_lvdm_ip_bans`                 |
	|       |                 |                 |          | (                                              |
	|       |                 |                 |          |     BANID INTEGER PRIMARY KEY,                 |
	|       |                 |                 |          |     ip TEXT,                                   |
	|       |                 |                 |          |     pla                                        |
	|-------|-----------------|-----------------|----------|------------------------------------------------|
	| table | lw_temp_data    | lw_temp_data    | 20549    | CREATE TABLE "lw_temp_data" (                  |
	|       |                 |                 |          | name text PRIMARY KEY,                         |
	|       |                 |                 |          | money INTEGER,                                 |
	|       |                 |                 |          | bank INTEGER,                                  |
	|       |                 |                 |          | bount                                          |
	\-------------------------------------------------------------------------------------------------------/
db_print_result

pawn Код:
stock db_print_result(DBResult:dbrResult, iMaxFieldLength = 20) {
    const
        MAX_ROWS         = 100,
        MAX_FIELDS       = 20,
        MAX_FIELD_LENGTH = 88
    ;
   
    static
        s_aaszFields[MAX_ROWS + 1][MAX_FIELDS][MAX_FIELD_LENGTH char],
        s_aiFieldMaxLength[MAX_FIELDS],
        s_szBuffer[(MAX_FIELD_LENGTH + 4) * MAX_FIELDS]
    ;
   
    static const
        szcSpacePadding[MAX_FIELD_LENGTH] = {' ', ...},
        szcDashPadding[MAX_FIELD_LENGTH] = {'-', ...}
    ;
   
    if (iMaxFieldLength == -1)
        iMaxFieldLength = MAX_FIELD_LENGTH;
   
    print(!" ");
    print(!"Query result:");
   
    if (!dbrResult)
        print(!"\t- No result.");
    else if (!db_num_rows(dbrResult))
        print(!"\t- No rows.");
    else {
        new
                 iRow = 0,
                 iRows,
                 iFields = db_num_fields(dbrResult),
                 iField,
                 iLength,
            bool:bHasMoreLines,
                 iPos,
                 iNextPos
        ;
       
        if (iMaxFieldLength > MAX_FIELD_LENGTH) {
            printf("\t- The longest possible field length is %d. Change MAX_FIELD_LENGTH for larger values.", MAX_FIELD_LENGTH);
           
            iMaxFieldLength = MAX_FIELD_LENGTH;
        }
       
        if (iFields > MAX_FIELDS) {
            printf("\t- There are %d, but only %d of them will be visible.", iFields, MAX_FIELDS);
            print(!"\t- Increase MAX_FIELDS if you want to see all fields.");
           
            iFields = MAX_FIELDS;
        }
       
        for (iField = 0; iField < iFields; iField++) {
            db_field_name(dbrResult, iField, s_szBuffer, iMaxFieldLength - 1);
           
            iPos = 0;
           
            while (-1 != (iPos = strfind(s_szBuffer, "\r", _, iPos)))
                s_szBuffer[iPos] = ' ';
           
            iPos = 0;
           
            while (-1 != (iPos = strfind(s_szBuffer, "\t", _, iPos))) {
                s_szBuffer[iPos] = ' ';
               
                strins(s_szBuffer, "   ", iPos, iMaxFieldLength);
               
                iPos += 4;
            }
           
            iPos = 0;

            do {
                iNextPos = strfind(s_szBuffer, "\n", _, iPos) + 1;

                if (!iNextPos)
                    iLength = strlen(s_szBuffer[iPos]);
                else
                    iLength = iNextPos - iPos - 1;

                s_aiFieldMaxLength[iField] = min(iMaxFieldLength, max(iLength, s_aiFieldMaxLength[iField]));
            } while ((iPos = iNextPos));
           
            strpack(s_aaszFields[0][iField], s_szBuffer, iMaxFieldLength char);
        }
       
        do {
            for (iField = 0; iField < iFields; iField++) {
                db_get_field(dbrResult, iField, s_szBuffer, iMaxFieldLength - 1);
               
                iPos = 0;
               
                while (-1 != (iPos = strfind(s_szBuffer, "\r", _, iPos)))
                    s_szBuffer[iPos] = ' ';
               
                iPos = 0;
               
                while (-1 != (iPos = strfind(s_szBuffer, "\t", _, iPos))) {
                    s_szBuffer[iPos] = ' ';
                   
                    strins(s_szBuffer, "   ", iPos, iMaxFieldLength);
                   
                    iPos += 4;
                }
               
                iPos = 0;

                do {
                    iNextPos = strfind(s_szBuffer, "\n", _, iPos) + 1;

                    if (!iNextPos)
                        iLength = strlen(s_szBuffer[iPos]);
                    else
                        iLength = iNextPos - iPos - 1;

                    s_aiFieldMaxLength[iField] = min(iMaxFieldLength, max(iLength, s_aiFieldMaxLength[iField]));
                } while ((iPos = iNextPos));

                strpack(s_aaszFields[iRow + 1][iField], s_szBuffer, iMaxFieldLength char);
            }
           
            if (++iRow >= MAX_ROWS) {
                iRows = iRow;
               
                while (db_next_row(dbrResult))
                    iRows++;
               
                printf("\t- Only the first %d rows are displayed; there are %d remaining.", MAX_ROWS, iRows);
               
                break;
            }
        } while (db_next_row(dbrResult));
       
        print(!" ");
       
        for (iRows = iRow, iRow = 0; iRow <= iRows; iRow++) {
            do {
                bHasMoreLines = false;
               
                s_szBuffer[0] = 0;
               
                for (iField = 0; iField < iFields; iField++) {
                    if (iField)
                        strcat(s_szBuffer, " | ");
                   
                    iLength = strlen(s_szBuffer);
                   
                    if (-1 != (iPos = strfind(s_aaszFields[iRow][iField], "\n"))) {
                        strunpack(s_szBuffer[iLength], s_aaszFields[iRow][iField], strlen(s_szBuffer[iLength]) + iPos + 1);
                       
                        strdel(s_aaszFields[iRow][iField], 0, iPos + 1);
                       
                        bHasMoreLines = true;
                    } else {
                        if (s_aaszFields[iRow][iField]{0}) {
                            strunpack(s_szBuffer[iLength], s_aaszFields[iRow][iField], sizeof(s_szBuffer) - iLength);
                           
                            s_aaszFields[iRow][iField]{0} = 0;
                        }
                    }
               
                    iLength = strlen(s_szBuffer[iLength]);
                   
                    strcat(s_szBuffer, szcSpacePadding, strlen(s_szBuffer) + (s_aiFieldMaxLength[iField] - iLength + 1));
                }
               
                if (bHasMoreLines)
                    printf("\t| %s |", s_szBuffer);
               
            } while (bHasMoreLines);
           
            if (iRow == 0) {
                printf("\t/ %s \\", s_szBuffer);
            } else {
                printf("\t| %s |", s_szBuffer);
            }
           
            if (iRow == iRows) {
                s_szBuffer[0] = 0;
               
                for (iField = 0; iField < iFields; iField++) {
                    if (iField)
                        strcat(s_szBuffer, "---");
                   
                    strcat(s_szBuffer, szcDashPadding, strlen(s_szBuffer) + s_aiFieldMaxLength[iField] + 1);
                }

                printf("\t\\-%s-/", s_szBuffer);
            } else {
                s_szBuffer[0] = 0;
               
                for (iField = 0; iField < iFields; iField++) {
                    if (iField)
                        strcat(s_szBuffer, "-|-");
                   
                    strcat(s_szBuffer, szcDashPadding, strlen(s_szBuffer) + s_aiFieldMaxLength[iField] + 1);
                }
               
                printf("\t|-%s-|", s_szBuffer);
            }
        }
    }
   
    print(!" ");
}
Reply

Nice function Slice, I'll try it with my sf-cnr gamemode

___

pawn Код:
#define TimeStampPassed(%1,%2)      ((GetTickCount()-%1)>(%2)) // TimeStampPassed(timestampVar, seconds
Usage:
pawn Код:
public OnPlayerUpdate(playerid)
{
    if(TimeStampPassed(p_WeaponUpdate[playerid], 250))
    {
        p_WeaponUpdate[playerid] = GetTickCount();
        SendClientMessage(playerid, -1, "Fart");
    }
    return 1;
}
Simple little macro instead of using timers
Reply

Encrypt and Decrypt

Introduction
Simple encryption technique based on Caesar's cipher.

How does it work?
Replaces a character according to the shift parameter.

Functions
pawn Код:
stock encrypt(string[],shift)
{
    for(new i=0; string[i] != '\0'; ++i)
        string[i]+=shift;
}
pawn Код:
stock decrypt(string[],shift)
{
    for(new i=0; string[i] != '\0'; ++i)
        string[i]-=shift;
}
Example
pawn Код:
main()
{
    new string[] = "This is a string.";
    encrypt(string, 13);
    printf("Encrypted String: %s", string);
    decrypt(string, 13);
    printf("Decrypted String: %s", string);
}
Reply

Quote:
Originally Posted by Gh05t_
Посмотреть сообщение
Encrypt and Decrypt

Introduction
Simple encryption technique based on Caesar's cipher.

How does it work?
Replaces a character according to the shift parameter.

Functions
pawn Код:
stock encrypt(string[],shift)
{
    for(new i=0; string[i] != '\0'; ++i)
        string[i]+=shift;
}
pawn Код:
stock decrypt(string[],shift)
{
    for(new i=0; string[i] != '\0'; ++i)
        string[i]-=shift;
}
Example
pawn Код:
main()
{
    new string[] = "This is a string.";
    printf("Original String: %s", string);
    encrypt(string, 13);
    printf("Encrypted String: %s", string);
    decrypt(string, 13);
    printf("Decrypted String: %s", string);
}
Hmm.. what could this be for? Is this even secure?
Reply

pawn Код:
#define DecreasePvar(%0,%1,%2) SetPVarInt(%0, %1, GetPVarInt(%0, %1) - %2)
Reply

Why not capitalizing V? Like DecreasePVar.
And if we have DecreasePVar, we could have IncreasePVar as well:
pawn Код:
#define IncreasePVar(%0,%1,%2) SetPVarInt(%0, %1, GetPVarInt(%0, %1) + %2)
#define DecreasePVar(%0,%1,%2) IncreasePVar(%0, %1, -(%2))
Reply


Forum Jump:


Users browsing this thread: 22 Guest(s)