943,996 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1169
  • C++ RSS
Jun 6th, 2007
0

Question, basic encrpytion?

Expand Post »
i am trying to write a program that takes information from a file and reads it and then does some kind of encryption, simple as adding one to it

I have it reading the file,
but how would i go about readings the characters as int's
and then adding a number to them?

example:
"Hi blah blah" = 234234 345634634 345345345 345345345
I want this to be stored in their number form + some number.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MrMan2787 is offline Offline
5 posts
since Jun 2007
Jun 6th, 2007
0

Re: Question, basic encrpytion?

char data type is a small int whose value is between -126 and 127. So do do what you want just loop through the array and add 1 to the char value.
C++ Syntax (Toggle Plain Text)
  1. char str[] = "Hi blah blah";
  2. // add 1 to the first character, syntax is idental to int
  3. ++str[0];
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Jun 6th, 2007
0

Re: Question, basic encrpytion?

Thanx, hmm but i still have one problem...

I need to read the text from a file.

So is there a way to read from the file and
then put that data into an array?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MrMan2787 is offline Offline
5 posts
since Jun 2007
Jun 6th, 2007
0

Re: Question, basic encrpytion?

after reading the string from a text file to the encryption like I posted earlier. Do you know how to read files ? In c++ use the ifstream class to read a text file. Here is an example of how to read/write text files.
Last edited by Ancient Dragon; Jun 6th, 2007 at 11:53 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Jun 6th, 2007
0

Re: Question, basic encrpytion?

This is the code i have so far, which only displays "file.txt" and displays it.


C++ Syntax (Toggle Plain Text)
  1. #include<iostream>
  2. #include<fstream>
  3.  
  4. using namespace std;
  5.  
  6. const int SIZEMAX = 81;
  7.  
  8. void read(fstream &);
  9.  
  10. int main()
  11. {
  12.  
  13. fstream dataFile;
  14.  
  15. dataFile.open("file.txt", ios::in);
  16. if(dataFile.fail())
  17. {
  18. cout << "The file does not exist. Please check your file."
  19. << endl;
  20.  
  21. return 0;
  22. }
  23.  
  24.  
  25. read(dataFile);
  26.  
  27. dataFile.close();
  28. cout << endl << "Done" << endl;
  29.  
  30.  
  31. return 0;
  32.  
  33. }
  34.  
  35.  
  36. void read(fstream &file)
  37. {
  38. char line[SIZEMAX];
  39.  
  40. while( file >> line)
  41. {
  42. cout << line << endl;
  43. }
  44. }
p.s. is there a way to tab in this chat box?
Last edited by Ancient Dragon; Jun 7th, 2007 at 7:09 am. Reason: replaced quote tags with code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MrMan2787 is offline Offline
5 posts
since Jun 2007
Jun 7th, 2007
0

Re: Question, basic encrpytion?

Well, this isn't really encryption, but I can't really figure out what you're trying to do to obtain the numbers. This just prints out the ASCII value for each character by casting to int.

C++ Syntax (Toggle Plain Text)
  1. int i = 0;
  2. while(myString[i]) {
  3. std::cout << static_cast<int>(myString[i]) << " ";
  4. i++;
  5. }
  6. std::cout << std::endl;
This code is fairly clear; it is trivial to modify it to fit into your read() function.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Jun 7th, 2007
0

Re: Question, basic encrpytion?

at line 42 add another loop to encrypt the line as suggested earlier then output the result to the screen as illustrated by joe.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Where to get C++ GUI prgramming tutorials?
Next Thread in C++ Forum Timeline: My solution to 2 beginner questions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC