# Binary Convertor Function

• 10-23-2003
AsmLover
Binary Convertor Function
I havn't programmed in ages and need some help plz, i needa write a function that scans all the characters in a text file and converts the characters into binary with this form of 8 digit format like so "01100001 ".....thankyou alot!
• 10-23-2003
Sebastiani
>> I havn't programmed in ages...

Good, then I'm sure you're ready to jump right into it! The purpose of this board is not to provide answers to homework. Try to get this to work on your own. If you are honestly stumped at that point, feel free to post 'specific' questions.

Happy coding. :)
• 10-24-2003
WaltP
Re: Binary Convertor Function
Quote:

Originally posted by AsmLover
I havn't programmed in ages and need some help plz, i needa write a function that scans all the characters in a text file and converts the characters into binary with this form of 8 digit format like so "01100001 ".....thankyou alot!
The request in blue comes up a lot. Do you understand the relationship between binary and a character value? What format do you need the binary in, an integer, byte, character array? A single byte? An int? Or just displayed on the screen?

The answer to this will help us figure out what you really need.
• 10-24-2003
DrZoidberg
You can use fopen to open a file and fgets to read a specific number of characters from the file.

char buffer[100];
FILE f = fopen("filename","r");
fgets(buffer, 100, f);

btw. characters are already stored in binary format.
• 10-29-2003
AsmLover
ok, i decided to change my encryption algorithm and i'm trying to use a basic rot13 function.......here we go

Code:

```#include <stdio.h> char filen; char rot(char ch); main(void) { FILE *fp; char ch; puts("\nFilename: ");         if((fgets(filen, sizeof(filen), stdin)==NULL) {             printf("Enter file name: ");             exit(1);         else if((fp = fopen(filen, "r")) ==NULL) {             printf("Cannot open %s", filen);             exit(1);         else fp = fopen(filen, "rw")               rot(ch){while(ch=~getchar(fp))putchar(~ch-1/(~(ch|32)/13*2-11)*13);}               fclose(filen);             }```
• 10-30-2003
Sebastiani
Quite a few problems there.

1) A char can only hold a single character. You need an array of them.

2) fgets() rarely returns NULL from stdin. A better approach is to check if the first character of the buffer is '\n', that means the user simply typed nothing (and presumably wishes to exit).

3) You can't define a function from within a function (main() in this case).

4) You can't open a file that's already open. Close it first.

5) If you're trying to represent a byte as 8 individual bytes, the
rot() function obviously won't work. You'll have to rethink your strategy.

Here's a starting framework:

Code:

```#include <stdlib.h> #include <stdio.h> #define MAX 1024 int rot_file(const char filename[]); int main(void) {  char filen[MAX];  FILE *fp;  printf("Enter file name: ");  fgets(filen, sizeof(filen), stdin);     if(filen[0] == '\n') exit(EXIT_SUCCESS);     if(!rot_file(filen))   {     printf("Problem processing file '%s'", filen);     exit(EXIT_FAILURE);   }    return EXIT_SUCCESS; } int rot_file(const char filename[]) {  int ch; // we need an int because a char can't hold EOF (usually -1)  FILE * fp = fopen(filename, "r");     if(fp)   {       while(EOF != (ch = fgetc(fp)))       {       /* do calculation & print to screen */       }             fclose(fp);   }     else return 0;      return 1; }```