Thread: Input in binary???

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    Input in binary???

    Hi all, I have a project where we're implementing a polynomial checksum. My issue is that the input file we're given is ten lines of what are supposed to be 32-bit binary numbers, i.e.

    10110001110001110101010100011001
    10010101010101010001010111011110
    10110011110001110101010100001101
    11100101111101010001000101011010
    11010101110001110101010100010101
    10011011010101010101010101111001
    10110001101101110101010100010000
    10010100110001010101010111011110
    10110001110101010101010101011011
    10110001010000010101010100111101

    What I'm stuck on is how to read these numbers from the file and treat them as binary. Any help would be appreciated.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Pick a forum and stick with it.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Just use strtoul():
    Code:
    {
      FILE *fp;
      char line[BUFSIZ];
      unsigned long num;
    
      fp = fopen("input.txt", "r");
      while(fgets(line, sizeof(line), fp))
      {
        num = strtoul(line, NULL, 2);
        printf("%lu\n", num);
      }
    
      fclose(fp);
    }
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The general approach is to process the file line by line. You'll need to move left to right through each line and use a combination of the left shift operator, << and the bitwise OR operator, |, and the constant 1. You'll also need a 32-bit unsigned number initialized to zero for each line.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Okay, I get it. Won't cross-post any more, I just wasn't sure if I would get an answer. Don't have to harp on it. My bad.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting input from user(character) converting to binary
    By saravana in forum C Programming
    Replies: 4
    Last Post: 09-24-2009, 12:47 PM
  2. I need help with decimal to binary Algorithm
    By webznz in forum C Programming
    Replies: 4
    Last Post: 03-13-2008, 03:52 AM
  3. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  4. data input in binary format
    By aamirsyed in forum C Programming
    Replies: 1
    Last Post: 06-30-2004, 05:27 PM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM