Thread: Binary Convertor Function

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    9

    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!

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> 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.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: Binary Convertor Function

    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.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    49
    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.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    9
    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);
                 
    
    
    }

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 02:02 PM
  2. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM