AutoHotkey Community

It is currently May 25th, 2012, 12:53 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: July 25th, 2007, 10:36 am 
Offline

Joined: June 16th, 2007, 6:43 pm
Posts: 35
I am trying to convert a c++ program I once wrote for small encryption/decryption for relaying messages. I am now trying to do the same in AHK but am completely lost.
The c++ code is
Code:
#include <iostream>
#include <string.h>
#include <cctype>

using namespace std;

char d[1024];
int l,i,c;

void out(),shift();

int main()
{
    system("cls");
    cout<<"\n\tPlease enter the text to be decoded :\n\t";
    cin.getline(d,1024);
    cout<<"\n\tPlease enter the value of character shift : ";
    cin>>c;
    if (c<0) c+=13*c*(-1)+(13-2*c);
    else c-=13*c-(13-2*c);
    l=strlen(d);
    shift();
    cout<<"\n\n\tDecoded text :\n\t";
    out();
    cout<<"\n\n\t";
    system("pause");
   return 0;
}

void out()
{
    for (i=0;i<l;i++)
    {
        cout<<d[i];
    }
}

void shift()
{
    int j,b=0;
    if (c<0)
    {
        c=c*(-1);
        b=1;
    }
    for (j=0;j<c;j++)
    {
        switch(b)
        {
            case 0:
            for (i=0;i<l;i++)
            {
                if ((d[i]>='a' && d[i]<'z') || (d[i]>='A' && d[i]<'Z')) d[i]+=1;
                else if (d[i]=='z') d[i]='a';
                else if (d[i]=='Z') d[i]='A';
                else if (d[i]>='0' && d[i]<'9') d[i]+=1;
                else if (d[i]=='9') d[i]='0';
            }
            break;
            case 1:
            for (i=0;i<l;i++)
            {
                if ((d[i]>'a' && d[i]<='z') || (d[i]>='A' && d[i]<='Z')) d[i]-=1;
                else if (d[i]=='a') d[i]='z';
                else if (d[i]=='A') d[i]='Z';
                else if (d[i]>'0' && d[i]<='9') d[i]-=1;
                else if (d[i]=='0') d[i]='9';
            }
        }
    }
}


The key (or shift as I've called it in the c++ code) is supposed to be numerical only.
What I've managed to code so far is nearly useless -

Code:
inputbox, text, Text, Please enter the text to be encoded/decoded
inputbox, key, Key, Please enter the key

if key<0
   key := %key% + 13 * %key% * ( - 1 ) + ( 13 - 2 * %key% )
else
   key := %key% - 13 * %key% - ( 13 - 2 * %key% )

text := regexreplace(text,"a",a+%key%)

inputbox, text, Output, The text after applying the key is,,,,,,,,%text%
inputbox, key, Key, Value of key after the operation,,,,,,,,%key%


If you try running this code then you'll see that entering a negative value of key gives an error, I narrowed it down to having used the %key% in the expression for changing the value of key when it is negative. If I enter a positive value of key, the key ends up blank.

Also, the character shift method I'm trying to do does not seem to be working here.


Last edited by Sukarn on July 25th, 2007, 12:50 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 25th, 2007, 11:53 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8775
Search the forum for Rot13 and Rot47. :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 25th, 2007, 4:23 pm 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8647
Location: Salem, MA
you don't need %% in expressions

Code:
inputbox, text, Text, Please enter the text to be encoded/decoded
inputbox, key, Key, Please enter the key

if key<0
   key := key + 13 * key * ( - 1 ) + ( 13 - 2 * key )
else
   key := key - 13 * key - ( 13 - 2 * key )

text := regexreplace(text,"a",a+key)

inputbox, text, Output, The text after applying the key is,,,,,,,,%text%
inputbox, key, Key, Value of key after the operation,,,,,,,,%key%

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 25th, 2007, 4:42 pm 
Offline

Joined: June 16th, 2007, 6:43 pm
Posts: 35
engunneer wrote:
you don't need %% in expressions

Code:
inputbox, text, Text, Please enter the text to be encoded/decoded
inputbox, key, Key, Please enter the key

if key<0
   key := key + 13 * key * ( - 1 ) + ( 13 - 2 * key )
else
   key := key - 13 * key - ( 13 - 2 * key )

text := regexreplace(text,"a",a+key)

inputbox, text, Output, The text after applying the key is,,,,,,,,%text%


Thanks, but the output still removes all the "a"s that exist in the input. I tested it by keeping some constant values of "key" like 1,5,-7.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 25th, 2007, 4:52 pm 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8647
Location: Salem, MA
what are you trying to do with the regex replace?

Code:
text := regexreplace(text,"a","a" . key)   ;. means concatenate.


and if you are not doing actual regex, you can use StringReplace

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 26th, 2007, 9:56 am 
Offline

Joined: June 16th, 2007, 6:43 pm
Posts: 35
engunneer wrote:
what are you trying to do with the regex replace?

Code:
text := regexreplace(text,"a","a" . key)   ;. means concatenate.


and if you are not doing actual regex, you can use StringReplace


I'm trying to shift the character like i've done in the c++ code. For example, if the input is "a", and the value of key after processing is 2, then "a" should become "c".
I'm now feeling as if this might not be possible as the conditions I placed in the c++ code might not be possible in AHK.
If I get this working, I would like to expand it from "a" to [a-zA-Z0-9], but this doesn't seem possible to me without doing it separately for all the characters. Of course, regex confuses me a lot and I'm quite new to it.
I'm obviously not doing it right if its possible to do with regexreplace. There should be some way to go about this.

Using StringReplace or RegExReplace seems to make no difference to me so far as for both of them I would have to write the code for each character separately, which isin't even working for one character right now.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 26th, 2007, 4:04 pm 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8647
Location: Salem, MA
ok. you can directly translate I think into Chr() and Asc() - that lets you see what the ascii code of the char is, and += or -= 1 to it to alter the char.

you probably need also a Loop, parse, with no delimiters, so you can work on the string one char at a time. It would take the place of the for(j=0;j<c;j++) loop.

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 26th, 2007, 5:13 pm 
Offline

Joined: June 16th, 2007, 6:43 pm
Posts: 35
engunneer wrote:
ok. you can directly translate I think into Chr() and Asc() - that lets you see what the ascii code of the char is, and += or -= 1 to it to alter the char.

you probably need also a Loop, parse, with no delimiters, so you can work on the string one char at a time. It would take the place of the for(j=0;j<c;j++) loop.


Yes, that's exactly what I needed. Thanks.
I had been searching for "string" and "array" with no success.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 26th, 2007, 6:06 pm 
Offline

Joined: June 16th, 2007, 6:43 pm
Posts: 35
I hope thes will be my last question. I've made up this rough program -
Code:
inputbox, text, Text, Please enter the text to be encoded/decoded
inputbox, key, Key, Please enter the key

if key<0
   key += 13 * key * ( - 1 ) + ( 13 - 2 * key )
else
   key -= 13 * key - ( 13 - 2 * key )

b := 0

if key < 0
{
   key *= (-1)
   b := 1
}

Loop, Parse, text
{
   temp = %A_LoopField%
   transform, temp2, asc, %A_LoopField%
   Loop key
   {
      if b = 0
      {
         temp2 += 1
         if temp2 = 141
            temp2 = 50
      }
      else if b = 1
      {
         temp2 -= 1
         if temp2 = 49
            temp2 = 140
      }
   }
   transform, temp3, chr, temp2
}

inputbox, text, Output, The text after applying the key is,,,,,,,,%text%


Now my problem is how do I replace the character that a_loopfield contains with the one in temp3.
From a_index (in the outer loop), I can find out which character in the string is currently being modified. My question is, is there anything to change the character at position a_index in the string text with the character in temp3?
In c++ this would have been -
Code:
text[%a_index%] = temp3

Once this is done, it will be way to easy to make this program compliant with the c++ one.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 26th, 2007, 6:57 pm 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8647
Location: Salem, MA
one way:
Code:
index = 5     ;(set to a_index)
temp3 = q
text = abcdefghijklmnopqrstuvwxyz

text := substr(text, 1, index-1) . temp3 . substr(text, index+1)

msgbox, %text%

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: mc-lemons, netaware, Yahoo [Bot] and 19 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group