-1

I am really new to coding and I have to do this project for school. We were required to encrypt a paragraph into binary and put that into a .out file. I did that, but now I have to take that .out file and work 'backwards' to translate that .out file from binary back into ASCII. We are not allowed to use math.h or stdlib.h I am currently trying:

#include <stdio.h>


int main()
{
  char x = 0;
  int i;
  int asciiVal = 0;
  while(x != EOF)
  {
    int binary[8];
    for(i = 7; i >= 0; i--)
    {
      asciiVal += (1 << i) * binary[7 - i];
    }
    return asciiVal;
  }
  return 0;
}

This is the encryption I did which works:

int main(void)
{
  char x = 0;
  int i;
  while(x != EOF)
  {
    x = getchar();
    for(i = 0; i < 8; i++)
    {
      putchar((x & (1 << i)) ? '1' : '0');
    }
    putchar('\n');
  }
  return 0;
}

I should also mention that I am getting an error with my array as it says "may be used uninitialized in this function." What I really don't understand is how to read the file 8 bits at a time, translate them into decimal, and then into ASCII.

  • 1
    `char` is not able to store `EOF`, switch to `int c = 0;` But why are you checking for `EOF` if you are not using any input function, do you mean `while((x = getchar()) != EOF)`? But wait, `x` is not used in the function, you can skip the entire `while` loop. – David Ranieri Feb 08 '21 at 20:56
  • There is no universal encryption algorithm. So you need to tell us what that is so we can work out what the decryption needs to be. *may be used uninitialized in this function* is probably due to the fact that `binary` contents are not set. You are supposed to read that from a file. Not sure whether you left that out of the example intentionally or not. Do you know how to open a file and read its contents? Or is that the very thing you are asking about? – kaylum Feb 08 '21 at 20:57
  • @DavidRanieri I used char to store EOF while encrypting. I used char x = 0; then i did while(x != EOF){ x = getchar(); and then a forloop which is: for(i = 0; i < 8; i++){ putchar((x & (1 << i)) ? '1' : '0'); – Bushra Elfarissi Feb 08 '21 at 21:00
  • @BushraElfarissi then you need to reflect that in your code, show a valid version of your code if you want some useful help. – David Ranieri Feb 08 '21 at 21:02
  • @kaylum I tagged David Ranieri on how I did the first encryption which worked for me and gave me a .out file with a bunch of 1s and 0s. I know how to read from a file but I am more confused on how to change from binary (reading the file 8 bits at a time) to decimal and then to ascii. – Bushra Elfarissi Feb 08 '21 at 21:03
  • What's the input and the expected result? – nicomp Feb 08 '21 at 21:08
  • 1
    Important info like that should not be in comments. [Edit](https://stackoverflow.com/posts/66109207/edit) your question to update it. If you know how to read from a file then show that in your code. Without that there is alot of time wasted trying to work out what it is you do know and what you actually need help with. – kaylum Feb 08 '21 at 21:09
  • We need to see the encryption method. It would be better if you could _edit_ your question and post the code [indented] in a code block rather than just flat in a comment. BTW, this really isn't an "encryption" method. It's outputting a string of printable ASCII ones and zeros that correspond to the binary bits in the input file. The reversal is slightly more complex but not that much. – Craig Estey Feb 08 '21 at 21:15
  • @DavidRanieri `char` is not able to store `EOF`? Are you sure about that? – alex01011 Feb 08 '21 at 21:16
  • 2
    @alex01011 Yes, David is correct. `EOF` is `-1` or `0xFFFFFFFF`. With `char`, it could be confused with a _valid_ 0xFF input char – Craig Estey Feb 08 '21 at 21:19
  • Just to clarify, and unless I'm wrong, this should be clearly stated in your question: this is not really an encryption, but just an encoding: the ASCII code of each character has been stored in the .out file as a sequence of eight 0/1 characters, from LSB to MSB. OP is asked to retrieve the original string by reading a bunch of eight 0/1 chars and reassembling them so as to get back the original ASCII code, and so on until EOF. @Bushra: I think that first and foremost, you should learn more about what is a C char, an ASCII code and the binary representation of a byte. – xhienne Feb 08 '21 at 21:20
  • @alex01011 yes, here you have a nice explanation: https://stackoverflow.com/q/4705968/1606345 – David Ranieri Feb 08 '21 at 21:20
  • @kaylum i didnt know i could edit the question, will do – Bushra Elfarissi Feb 08 '21 at 22:19

1 Answers1

0

Your decrypt didn't do any input/output.

Because your encrypt program outputs a line for each input byte, it's easier for the decrypt program to read a line at a time (e.g. fgets) and then loop on the 8 byte buffer.

Unfortunately, your encrypt program actually had a bug. Because the while (x != EOF) that was followed by x = getchar(), the test for EOF comes too late and the program will output a superfluous 11111111 line at the end. And, myself and others have mentioned, x should be int and not char


Here's the refactored decrypt program:

#include <stdio.h>
#include <stdlib.h>

int
main()
{
    int i;
    unsigned char x;
    unsigned char y;
    char line[10];

    while (fgets(line,sizeof(line),stdin) != NULL) {
        y = 0;

        for (i = 0; i < 8; i++) {
            y <<= 1;
            x = line[i];

            switch (x) {
            case '0':
                break;

            case '1':
                y |= 1;
                break;

            default:  // bad character (not '0' or '1')
                fprintf(stderr,"error on input -- %2.2X\n",x);
                exit(1);
                break;
            }
        }

        // ensure line is correct length and terminated
        x = line[i];
        if (x != '\n') {
            fprintf(stderr,"error on input -- %2.2X\n",x);
            exit(1);
        }

        // output the binary byte
        putchar(y);
    }

    return 0;
}

Here's the refactored encrypt program:

#include <stdio.h>

int
main(void)
{
    int x;
    int i;

    while (1) {
        x = getchar();
        if (x == EOF)
            break;

        for (i = 0; i < 8; i++)
            putchar((x & (1 << i)) ? '1' : '0');
        putchar('\n');
    }

    return 0;
}
Craig Estey
  • 30,627
  • 4
  • 24
  • 48