GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Feb-2004, 14:34
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough

Simple Encryption Algorithm


Ok, I've made a very simple encryption algorithm that encrypts text one character at a time. Here's the code for my encryption function. It uses ASCII code to convert the character to an integer. I only wanted ASCII characters greater than or equal to "!", so I subtracted 33 from the code number to make things easier.

CPP / C++ / C Code:
int Encrypt(char a)
{
  if (a == ' ')
    return 95;
  else if (a == '\n')
    return 96;

  for (int x = 0; a != x; x++)
    {
      if (a-33 == x)
        return x;
    }
  return 0;
}

I made my own character references for spaces and newlines... just so I can keep track of them. My decryption algorithm is non-existent. I don't know how to convert a number back to a character based on ascii code... any ideas?
  #2  
Old 15-Feb-2004, 16:17
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,242
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Considering your code, I don't understand what you are asking. Your are correctly using the 'character' value as a number in your posted code. Why can't you simply reverse the process?

Your current code uses a loop:
CPP / C++ / C Code:
for (int x = 0; a != x; x++)
    {
      if (a-33 == x)
        return x;
    }
to return the value but all you really need is:
CPP / C++ / C Code:
if (a-33 >0)
   return a-33;
else
   return 0;
unless I'm missing something.
  #3  
Old 15-Feb-2004, 17:29
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Because you can't increment a letter. If I have a char variable x initialized to ascii letter 'a', I can't say x++ and get 'b', can I? Argh. My brain isn't working. All I want to do is be able to take a 2 digit number from a file, add 33 to it, and turn it into a char. Uh... did I just solve the problem?

Btw, I see how your idea works and it's much more efficient. Thanks
  #4  
Old 15-Feb-2004, 22:38
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,242
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by aaroncohn
Because you can't increment a letter. If I have a char variable x initialized to ascii letter 'a', I can't say x++ and get 'b', can I? Argh. My brain isn't working. All I want to do is be able to take a 2 digit number from a file, add 33 to it, and turn it into a char. Uh... did I just solve the problem?

Btw, I see how your idea works and it's much more efficient. Thanks
You're welcome, and of course you can:
CPP / C++ / C Code:
char ch;
for (ch = 'A'; ch <= 'Z'; ch++)
    printf("%c ", ch);
Prints the alphabet from A to Z

A letter is just a display of the value of the letter. For example the letter 'A' inside the computer is the integer value 65. If you output as an
int (%d), you get 65
char (%c) you get 'A'
hex value (%x), 41

Do whatever math you want on it. ('A' * 2) - 'H' = ':'

Expand the above code to print all the characters from 32 (space) to 255 (highest ascii value) in a chart. A very useful tool I use constantly.
  #5  
Old 16-Feb-2004, 01:42
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Quote:
Originally Posted by WaltP
You're welcome, and of course you can:
CPP / C++ / C Code:
char ch;
for (ch = 'A'; ch <= 'Z'; ch++)
    printf("%c ", ch);
Prints the alphabet from A to Z

A letter is just a display of the value of the letter. For example the letter 'A' inside the computer is the integer value 65. If you output as an
int (%d), you get 65
char (%c) you get 'A'
hex value (%x), 41

Do whatever math you want on it. ('A' * 2) - 'H' = ':'

Expand the above code to print all the characters from 32 (space) to 255 (highest ascii value) in a chart. A very useful tool I use constantly.

OK. I got the decryption algorithm to work, but Borland is giving me some really freaking weird errors that shouldn't be happening. Something keeps using the same memory space that my variables are using, because they keep getting wiped out. I have to make them static just so they don't get destroyed, which is highly unusual.
  #6  
Old 16-Feb-2004, 02:58
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,242
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by aaroncohn
OK. I got the decryption algorithm to work, but Borland is giving me some really freaking weird errors that shouldn't be happening. Something keeps using the same memory space that my variables are using, because they keep getting wiped out. I have to make them static just so they don't get destroyed, which is highly unusual.
It's probably just something you forgot about in some declaration somewhere. Or local variable in a function that are referenced after the function returns. Nothing too serious.
  #7  
Old 16-Feb-2004, 10:55
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Well I'm also getting errors when the program finishes. My little box pops up and says that the decryption completed successfully, then the program gives me a write to address error. But it only happens after my last line of code is executed. I checked out my variables and they are fine. The decryption works like a charm. The program just has a huge fit at the end for some really strange reason. I think my borland installation might have been corrupted...
  #8  
Old 16-Feb-2004, 14:00
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi aaron. It seems strange that you would be having memory allocation problems with this. If you get a chance, try to upload what you have. If it is kind of big, just upload it as an attachment.
  #9  
Old 17-Feb-2004, 11:48
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Quote:
Originally Posted by dsmith
Hi aaron. It seems strange that you would be having memory allocation problems with this. If you get a chance, try to upload what you have. If it is kind of big, just upload it as an attachment.

Do you have borland builder 6? This is a windows program built in borland builder 6. I bet I wouldn't have any problem at all if it were a console app with the same core code.
  #10  
Old 17-Feb-2004, 12:00
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Aaron. I am not so interesed in the interface specific stuff, but more the decryption routine and how you are calling it etc. I won't be able to build your project without some porting, but I (or someone else) may be able to see where a mem alloc error could be occuring.
 
 

Recent GIDBlogToyota - 2008 August Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
help.... SLR * algorithm tay C Programming Language 4 10-Sep-2004 11:48
Color scale algorithm Ilya C++ Forum 0 28-Oct-2003 11:07
algorithm question calculus87 C Programming Language 1 11-Oct-2003 09:24
simple form from generated page zuzupus Web Design Forum 0 17-Sep-2003 09:27
Generating simple URLs for search engines jrobbio MySQL / PHP Forum 1 17-Mar-2003 11:22

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:47.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.