Jump to content

Recommended Posts

Posted

Look at the function StringMid, use a for loop to go through the string one character at a time, then change the character to what you want it to be.

What would you want the result to be if the character is X Y or Z, because you'll end up with characters that aren't letters.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Posted
5 minutes ago, BrewManNH said:

Look at the function StringMid, use a for loop to go through the string one character at a time, then change the character to what you want it to be.

What would you want the result to be if the character is X Y or Z, because you'll end up with characters that aren't letters.

thank you and

when the character is x y or z it should be starting from the beginning of the alphabet

Posted

Here's a way of doing it that keeps it all letters, as long as the input is all letters that is.

Global $sString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

Global $aString = StringToASCIIArray($sString)
For $Loop = 0 To UBound($aString) - 1
    If $aString[$Loop] > 86 And ($aString[$Loop] < 91) Then ; character is W, X, Y, or Z
        $aString[$Loop] -= 26 ; reduce the number by 26
    EndIf
    If $aString[$Loop] > 118 Then ; Character is w, x, y, or z
        $aString[$Loop] -= 26 ; reduce the number by 26
    EndIf
    $aString[$Loop] = $aString[$Loop] + 4 ; add 4 to the ASCII code of the character
Next
Global $sNewString = StringFromASCIIArray($aString) ; Create a string of characters from an array of ASCII 
ConsoleWrite("$sString = " & $sString & @TAB & "$sNewString = " & $sNewString & @CRLF)

The 2 If statements could easily be combined into one if you wanted to, but I did 2 of them to show how I'm comparing the current character to the Upper or Lower case letters you don't want to get added to until they've been converted.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Posted (edited)

Should work...  :sweating:

$check = "abcdefghijklmnopqrstuvwxyz"
$step = 4

$string = "abcduvwxyz"

$string = Execute(StringRegExpReplace($string, '(.)', "StringMid($check, Mod(StringInStr($check, '$1')+$step-1, 26)+1, 1) & ") & "''")
Msgbox(0,"", $string)

; reverse
$string = Execute(StringRegExpReplace($string, '(.)', "StringMid($check, Mod(StringInStr($check, '$1')+26-$step-1, 26)+1, 1) & ") & "''")
Msgbox(0,"", $string)

 

Edited by mikell
Posted

This is something like Rot13 encoding. Just change 13 to 4

 

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Posted

Hi.

I updated the example @funkey linked to, to be more flexible:

#include <MsgBoxConstants.au3>
Local $iRotating = 4
Local $sInput = "dog"
Local $sOutput = _ROT13($sInput, $iRotating)
If @error Then
    MsgBox($MB_TOPMOST, 'Error', "Error occured: " & @error)
Else
    MsgBox($MB_TOPMOST, "Rotated", $sOutput) ; Returns "hsk" if $sInput = "dog" and $iRotating = 4
EndIf

Func _ROT13($sString, $iRotate = 13)
    Local $aLetters = StringSplit($sString, "")
    Local $sResult = "", $sChar
    For $i = 1 to $aLetters[0]
        $sChar = _TransChr($aLetters[$i], $iRotate)
        If @error Then Return SetError(2)
        $sResult &= $sChar
    Next
    Return $sResult
EndFunc

Func _TransChr($sChr, $iRotate = 13)
    If Not IsNumber($iRotate) Then ; but could be number in quotation marks
        If Number($iRotate) = $iRotate Then ; is a number in quotation marks
            $iRotate = Number($iRotate)
        Else
            Return SetError(1)
        EndIf
    EndIf
    $iRotate = Mod($iRotate, 26) ; reduces the multiples of the alphabet
    If StringRegExp($sChr, "[a-z]") Then ; all lower characters
        Local $aLower = StringSplit("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz","") ; lower characters two times
        For $i = 1 to 26
            If $sChr = $aLower[$i] Then
                Return $aLower[$i + $iRotate]
            EndIf
        Next
    ElseIf StringRegExp($sChr, "[A-Z]") Then ; all upper characters
        Local $aUpper = StringSplit("ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ","") ; upper characters two times
        For $i = 1 to 26
            If $sChr = $aUpper[$i] Then
                Return $aUpper[$i + $iRotate]
            EndIf
        Next
    Else
        Return $sChr
    EndIf
EndFunc

With $iRotating you can take whatever number you want.

Simpel

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Posted (edited)

And another one without an array:

#include <MsgBoxConstants.au3>

Local $iRotating = 4
Local $sInput = "Hello World! I hope you're all alright!"

Local $sOutput = _ROT13($sInput, $iRotating)
If @error Then
    ConsoleWrite("Error occured: " & @error & @CRLF)
Else
    ConsoleWrite("Input: " & $sInput & @CRLF)
    ConsoleWrite("Output: " & $sOutput & @CRLF)
    ConsoleWrite("Reverse: " & _ROT13($sOutput, $iRotating * (-1)) & @CRLF) ; reverse - you only have to multiply $iRotating with -1
EndIf


Exit

Func _ROT13($sString, $iRotate = 13)
    Local $aLetters = StringSplit($sString, "")
    Local $sResult = "", $sChar
    For $i = 1 to $aLetters[0]
        $sChar = _TransChr($aLetters[$i], $iRotate)
        If @error Then Return SetError(2)
        $sResult &= $sChar
    Next
    Return $sResult
EndFunc

Func _TransChr($sChr, $iRotate = 13)
    If Not IsNumber($iRotate) Then ; but could be number in quotation marks
        If Number($iRotate) = $iRotate Then ; is a number in quotation marks
            $iRotate = Number($iRotate)
        Else
            Return SetError(1)
        EndIf
    EndIf
    Local $iAsc, $iMod
    If StringRegExp($sChr, "[a-z]") Then ; all lower characters
        $iAsc = Asc($sChr) ; make Ascii
        $iMod = Mod($iAsc - 96 + $iRotate, 26) ; Ascii "a" = 97, so substract 96 to get 1; add Rotation and Modulus all to reduce the multiples of the alphabet
        If $iMod < 1 Then $iMod += 26
        $sChr = Chr($iMod + 96) ; add the 96 again and make the character of the ascii
    ElseIf StringRegExp($sChr, "[A-Z]") Then ; all upper characters
        $iAsc = Asc($sChr)
        $iMod = Mod($iAsc - 64 + $iRotate, 26) ; Ascii "A" = 65, so substract 64 to get 1
        If $iMod < 1 Then $iMod += 26
        $sChr = Chr($iMod + 64)
    EndIf
    Return $sChr
EndFunc

Maybe a bit faster. Simpel

Edited by Simpel
Add: reverse
SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Posted (edited)

also, extending the check the length of the step should satisfy reqs

$check = "abcdefghijklmnopqrstuvwxyz"
$step = 4

$check = $check & stringleft($check , $step)
$stringout=""

$string = "abcduvwxyz"

for $i = 1 to stringlen($string)
$stringout &= stringmid($check , stringinstr($check , stringmid($string , $i , 1)) + $step , 1 )
Next

msgbox(0, '' , $stringout)

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Posted (edited)

This appears to work.

Local $string = "ABCDUVWXYZ abcduvwxyz 012789 @#$!"
Local $iOffset = 4

; --------- Offset lower case characters only ---------
Local $stringModified = Execute(StringRegExpReplace($string, '(.)', _
        "(((AscW('\1') > 96) and (AscW('\1') < 123)) ? (ChrW(Mod(AscW('\1') - (97 - $iOffset), 26) + 97)) : '\1')&") & "''") ; Lower case (Asc('a') = 97)
MsgBox(0, "Results", $string & " = " & $stringModified)
; Returns: ABCDUVWXYZ abcduvwxyz 012789 @#$! = ABCDUVWXYZ efghyzabcd 012789 @#$!

; --------- Offset lower case, upper case, and digit characters only ---------
Local $stringModified2 = Execute(StringRegExpReplace($string, '(.)', "_CC('${1}',$iOffset)&") & "''") ; Upper case (Asc('A') = 65)
MsgBox(0, "Results", $string & " = " & $stringModified2)
; Returns: ABCDUVWXYZ abcduvwxyz 012789 @#$! = EFGHYZABCD efghyzabcd 456123 @#$!


; Change\Convert Character function
; $iOS - Offset number
Func _CC($sChar, $iOS) 
    Select
        Case (AscW($sChar) >= 97) And (AscW($sChar) <= 122)
            Return (ChrW(Mod(AscW($sChar) - (97 - $iOS), 26) + 97)) ; Lower case (Asc('a') = 97)
        Case (AscW($sChar) >= 65) And (AscW($sChar) <= 90)
            Return (ChrW(Mod(AscW($sChar) - (65 - $iOS), 26) + 65)) ; Upper case (Asc('A') = 65)
        Case (AscW($sChar) >= 48) And (AscW($sChar) <= 57)
            Return (ChrW(Mod(AscW($sChar) - (48 - $iOS), 10) + 48)) ; Digits (Asc('0') = 48)
        Case Else
            Return $sChar
    EndSelect
EndFunc   ;==>_CC

 

Edited by Malkey
In function used global variable instead of offset parameter, $iOS.
Posted (edited)

one more that subtracts stringlen($check) - $step when the matching character is at a position greater than $stringlen - $step+1.

$check = "abcdefghijklmnopqrstuvwxyz"
$step = 4
$stringout = ""
$string = "abcduvwxyz"

for $i = 1 to stringlen($string)
$stringout &= (stringinstr($check , stringmid($string , $i , 1)) < (stringlen($check) - $step + 1) ) ? (stringmid($check , stringinstr($check , stringmid($string , $i , 1)) + $step , 1)) : stringmid($check , stringinstr($check , stringmid($string , $i , 1)) - (stringlen($check) - $step) , 1)
Next

msgbox(0, '' , $stringout)

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Posted

@iamtheky : based on your script, with a "special alphabet"  :)

$alphabet = "abcdefghijklmnopqrstuvwxyzabcd"
$string_in = "abcduvwxyz"
$string_out = ""

For $i = 1 to stringlen($string_in)
   $string_out &= stringmid($alphabet, _
   4 + stringinstr($alphabet, stringmid($string_in, $i, 1)), _
   1)
Next

msgbox(0, "" , $string_out)

 

"I think you are searching a bug where there is no bug... don't listen to bad advice."

Posted

i suppose we could have been hardcoding the step all along based off the OP...  I blame mikell :)

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Posted (edited)

I accept the blame. The thing should work with $step = any positive integer indeed
:)

$check = "abcdefghijklmnopqrstuvwxyz :"
$n = StringLen($check)
$step = 2317

$string = "this is a string : uvwxyz"  

$string = Execute(StringRegExpReplace($string, '(.)', "StringMid($check, Mod(StringInStr($check, '$1')+$step-1, $n)+1, 1) & ") & "''")
Msgbox(0,"", $string)

; reverse
$string = Execute(StringRegExpReplace($string, '(.)', "StringMid($check, Mod(StringInStr($check, '$1')+$n*(Int($step/$n)+1)-$step-1, $n)+1, 1) & ") & "''")
Msgbox(0,"", $string)

Edit :
AND we should also provide the stuff to get the initial string back  :P

Edited by mikell
Posted
3 hours ago, mikell said:

AND we should also provide the stuff to get the initial string back  :P

Yes. In my version you have to execute the function again with the $iRotating multiplied by -1. Added that in the code above. Or you can better add a third variable to the function like $bReverse = False as default and do the multiplying inside the function if $bReverse is True.

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Posted (edited)

Encode and decode with a single simple function.
To decode just pass the encoded string and the inverse of the encoding key

Local $sOriginal = "the quick brown fox jumps over the lazy dog"
    Local $iKey = 2317
    Local $sEncoded = _Shift_chars($sOriginal, $iKey) ; encode a string
    ; to decode just pass the encoded string and the inverse of the encoding key
    Local $sDecoded = _Shift_chars($sEncoded, $iKey * - 1) ; decode a string
    ConsoleWrite("Original: " & $sOriginal & @CRLF & "Encoded : " & $sEncoded & @CRLF & "Decoded : " & $sDecoded & @CRLF)

Func _Shift_chars($sString, $iOffset = 0)
    Local Static $sAlphabet = ' abcdefghijklmnopqrstuvwxyz?', $aAlphabet = StringSplit($sAlphabet, '', 2), $iChars = UBound($aAlphabet)
    Local $sReturn = ''
    $iOffset = Mod($iOffset, $iChars) ; limit $iOffset within $iChars
    For $i = 1 To StringLen($sString)
        $sReturn &= $aAlphabet[Mod($iChars + (StringInStr($sAlphabet, StringMid($sString, $i, 1)) - 1 + $iOffset), $iChars)]
    Next
    Return $sReturn
EndFunc   ;==>_Shift_chars

 

Edited by Chimp
debug: added -> $iOffset = Mod($iOffset, $iChars) to listing

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Posted

Chimp,
Doesnt work with 2317  :hyper:

$string = "Hey, isn't it nice ? \o/ This is a ""string"" : (#404) uvw@xyz ! "  
; $string = FileRead("test.txt") ; no need to double quotation marks here 
Msgbox(0,"original", $string)

$string = _ShiftReplace($string, 2317) 
Msgbox(0,"shifted", $string)

$string = _ShiftReplace($string, -2317) 
Msgbox(0,"and back", $string)


Func _ShiftReplace($string, $step) 
   Local $check
   For $i = 32 to 126
       $check &= Chr($i)
   Next
   $check = StringReplace($check, "'", Chrw(0xfffd))
   $n = StringLen($check)
   $string = StringReplace($string, "'", Chrw(0xfffd))
   $bool = $step < 0 ? 0 : 1
   $step = Abs($step)

   If $bool = 1 Then
      $string = Execute(StringRegExpReplace($string, '(.)', "StringMid($check, Mod(StringInStr($check, '$1', 1)+$step-1, $n)+1, 1) & ") & "''")
   ElseIf $bool = 0 Then
      ; reverse
      $string = Execute(StringRegExpReplace($string, '(.)', "StringMid($check, Mod(StringInStr($check, '$1', 1)+$n*(Int($step/$n)+1)-$step-1, $n)+1, 1) & ") & "''")
   EndIf

   Return StringReplace($string, Chrw(0xfffd), "'")
EndFunc

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...