Merkkijonot : funktiot

ASC


   KUVAUS

Jokaisella tietokoneen merkistön merkillä (oli sitten kirjan, numero tai symboli) on ASCII-koodiksi kutsuttu tunniste. Toisin sanoen, jokaisella merkillä on myös oma numero (0-255). Tämä funktio on päinvastainen toimitus Chr:lle. Se palauttaa merkin ASCII-koodin. Esim. "A" -> 65.

Esimerkiksi GetKey palauttaa painetun näppäimen ASCII-merkin.

Asc- ja Chr-funktioita käytetään yleisesti yksinkertaiseen tiedon salaukseen.

Katso ASCII-taulukko.

Komento on tarkoitettu edistyneemmille käyttäjille. Sen hyöty tavalliselle peliohjelmoijalle jäänee vähäiseksi.

   KÄYTTÖ
ASC (merkki)

  • merkki = Yhden merkin merkkijono. Esim. "m"

  • Katso myös: CHR

       ESIMERKKI
    AddText "This example shows how to encypt a string"

    'Ask a string
    Repeat
        k$=Input("Give a sentence: ")
        DrawScreen
    Until KeyHit(cbkeyreturn)

    CloseInput
    ClearKeys

    'encrypt
    encrypted$=encrypt(k$,"secret")

    'decrypt
    decrypted$=decrypt(encrypted$,"secret")

    AddText "Original:"+k$
    AddText "Encrypted:"+encrypted$
    AddText "Decrypted:"+decrypted$

    Repeat
        DrawScreen
    Until EscapeKey()

    End

    '- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -

    'This function encrypts a string with a given password
    'The longer the password the better the encryption
    Function encrypt(stri$,pass$)
        passchar=1
        build$=""
        
        For i=1 To Len(stri$)
            newvalue=Asc(Mid(stri$,i,1))+Asc(Mid(pass$,passchar,1))
            If newvalue>255 Then newvalue=newvalue-255
            build$=build$+Chr(newvalue)
            
            passchar=passchar+1
            If passchar>Len(pass) Then passchar=1
        Next i
        
        Return build$
    End Function

    'This function decrypts already crypted password
    Function decrypt(stri$,pass$)
        passchar=1
        build$=""
        
        For i=1 To Len(stri$)
            newvalue=Asc(Mid(stri$,i,1)) -Asc(Mid(pass$,passchar,1))
            If newvalue<0 Then newvalue=newvalue+255
            build$=build$+Chr(newvalue)
            
            passchar=passchar+1
            If passchar>Len(pass) Then passchar=1
        Next i
        
        Return build$
    End Function

    <<TAKAISIN