Thread: still having trouble with ASCII chars and very simple encryption

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    150

    still having trouble with ASCII chars and very simple encryption

    I am trying to make a simple program that writes source code for a much larger program. I don't know what I am getting wrong. Here is my code it is a simple reversal:
    Code:
     #include int main() {     FILE *fp; fp=fopen("d:\\crypt.h", "w");  fprintf(fp, "char out[8];char cur[8];         char con[255,255,255,255,255,255,255,255];/n         if cur==con out==0,0,0,0,0,0,0,0;/n         while(cur =con out=out+1;           ");          return 0; }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    does anyone know why my code posts all bunched up on a single line? I am using Firefox but the code looks fine until I post it and I have tried editing it but the save button wont work.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Maybe try pasting your code from a plain-text editor like notepad. E.g. copy/paste from YourFavoriteEditor into notepad and then afterwards copy/paste from notepad into the browser. Maybe there is some kind of rich text formatting going on that is messing up the embedded editor. Also as previously mentioned click "Go advanced" in the edit screen to see a preview first.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    Code:
    #include<stdio.h>
    int main()
    {
        FILE *fp;
    fp=fopen("d:\\crypt.h", "w");
    
    fprintf(fp, "char out[8];char cur[8];"
            "char con[255,255,255,255,255,255,255,255];/n"
            "if cur==con out==0,0,0,0,0,0,0,0;/n"
            "while(cur <= con) /n"
            "con=con-1/n"
    
            "if cur>=con out=out+1;");
             return 0;
    }

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Looks a little better, you should work on the indentation. Comments:

    - You must close your file before you exit the program, otherwise the contents will be undefined if you tried to write to it.
    - For text files you should end with a newline.
    - When you open a file you need to make sure it actually worked. What if "d:\\crypt.h" is not writable?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        const char *filename = "c:\\crypt.h";
        FILE *fp;
        fp=fopen(filename, "w");
        if (fp == NULL) {
            perror(filename);
            exit(1);
        }
        fprintf(fp, "char out[8];char cur[8];"
            "char con[255,255,255,255,255,255,255,255];/n"
            "if cur==con out==0,0,0,0,0,0,0,0;/n"
            "while(cur <= con) /n"
            "con=con-1/n"
            "if cur>=con out=out+1;"
        );
        fprintf(fp, "\n");
        fclose(fp);
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    It writes to it one line of code that is, I just can't get this figured out-how to make it completely write itself for all the values.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    should I have declared some vars outside of the fprintf?

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Yes, it writes one line of code. The line terminator (newline character) is "\n" by the way not "/n". Maybe you want to change those to newline characters for multiple-line output.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    thanks I didn't catch that.

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    is it possible to compare char arrays? I read in a dummies book that you can't compare strings unless it is with strcmp and it returns 0 if it finds a match which if true means that I am doing it all wrong and should be comparing unsigned long longs. Is this what I should be doing is %llu?

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    I need the >< to work

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Once-ler View Post
    I read in a dummies book that you can't compare strings unless it is with strcmp and it returns 0 if it finds a match
    What do you mean when it finds a "match"? Maybe for learning you can use whatever book you want but to clarify meanings of return codes and parameters instead try a good function reference e.g. "man 3 strcmp"

    strcmp(3): compare two strings - Linux man page

  13. #13
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    Code:
     #include int main() {     unsigned long long count;     char output=5;      FILE *fp;          fp=fopen("d:\\crypt.h", "w");          (count & 1) ===>          fprintf(fp, "char out[8];unsigned long long cur;"         "unsigned long long con =18446744073709551615;\n"         "if cur==con out==255,255,255,255,255,255,255,255;\n"         "while(cur =con out=out+1;");          return 0; }
    would that be better?

  14. #14
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    Code:
      #include  int main()  {      unsigned long long count;      char output=5;       FILE *fp;           fp=fopen("d:\\crypt.h", "w");           (count & 1) ===>   fprintf(fp, "char out[8];unsigned long long cur;"          "unsigned long long con =18446744073709551615;\n"          "if cur==con out==255,255,255,255,255,255,255,255;\n"          "while(cur =con out=out+1;");           return 0; }
    would that be better? sorry about that would it be easier to do it this way?

  15. #15
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    Code:
      #include int main() {     unsigned long long count =18446744073709551615;     char output=5;      FILE *fp;          fp=fopen("d:\\crypt.h", "w");          while (count !=0);          fprintf(fp, "char out[8];unsigned long long cur;"         "unsigned long long con =;\n%llu");count;         "if cur==con out==255,255,255,255,255,255,255,255;\n"         "out=out-2";             "con=con-1\n";          "count=count-8\n";          printf("%llu",count);          return 0; }
    This is what I have after reading a book that said you can't compare strings or char arrays with the conditional statements. Can anyone help me get this thing come to life so it can output some code?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting ASCII text into chars or strings.
    By workisnotfun in forum C++ Programming
    Replies: 1
    Last Post: 04-24-2013, 07:02 PM
  2. Trouble printing an array of chars
    By ppata in forum C Programming
    Replies: 8
    Last Post: 11-11-2010, 09:39 PM
  3. I have big problem with encryption by ASCII
    By LINUX in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2007, 12:46 PM
  4. ascii and pointer trouble!!!
    By algi in forum C++ Programming
    Replies: 1
    Last Post: 11-16-2004, 03:00 PM
  5. converting chars to ascii equivilant
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-01-2001, 12:03 PM