AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Need help converting a c++ code

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Sukarn



Joined: 16 Jun 2007
Posts: 35

PostPosted: Wed Jul 25, 2007 9:36 am    Post subject: Need help converting a c++ code Reply with quote

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 Wed Jul 25, 2007 11:50 am; edited 1 time in total
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Wed Jul 25, 2007 10:53 am    Post subject: Reply with quote

Search the forum for Rot13 and Rot47. Smile
Back to top
View user's profile Send private message Send e-mail
engunneer



Joined: 30 Aug 2005
Posts: 8255
Location: Maywood, IL

PostPosted: Wed Jul 25, 2007 3:23 pm    Post subject: Reply with quote

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%

_________________

(Common Answers)
Back to top
View user's profile Send private message Visit poster's website
Sukarn



Joined: 16 Jun 2007
Posts: 35

PostPosted: Wed Jul 25, 2007 3:42 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
engunneer



Joined: 30 Aug 2005
Posts: 8255
Location: Maywood, IL

PostPosted: Wed Jul 25, 2007 3:52 pm    Post subject: Reply with quote

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
_________________

(Common Answers)
Back to top
View user's profile Send private message Visit poster's website
Sukarn



Joined: 16 Jun 2007
Posts: 35

PostPosted: Thu Jul 26, 2007 8:56 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
engunneer



Joined: 30 Aug 2005
Posts: 8255
Location: Maywood, IL

PostPosted: Thu Jul 26, 2007 3:04 pm    Post subject: Reply with quote

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.
_________________

(Common Answers)
Back to top
View user's profile Send private message Visit poster's website
Sukarn



Joined: 16 Jun 2007
Posts: 35

PostPosted: Thu Jul 26, 2007 4:13 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Sukarn



Joined: 16 Jun 2007
Posts: 35

PostPosted: Thu Jul 26, 2007 5:06 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
engunneer



Joined: 30 Aug 2005
Posts: 8255
Location: Maywood, IL

PostPosted: Thu Jul 26, 2007 5:57 pm    Post subject: Reply with quote

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%

_________________

(Common Answers)
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group