Thread: binary comparison

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    18

    binary comparison

    hi there...

    never done this before so a little bit confused...

    i'm dumping a binary (image) file bit by bit - one char per line - using fread. in the terminal i see something like:

    <9D> 1
    <A9> 2
    <9E> 3
    <D0> 4
    <83> 5
    ^@ 6
    ^X 7
    M 8
    4 9
    <DE> 10
    : 11
    ` 12
    <FF> 13
    <D8> 14
    <FF> 15
    <E1> 16
    ^U 17
    N 18
    E 19
    x 20
    ....
    ...
    ...


    the thing is i need to start reading - in this case - line 13. i'm looking for the <FF> line. the <FF> char is not always on the same line..

    the code i use to get the output above is:

    Code:
    FILE * fhin = fopen (argv[1], "rb");
            char c;
            int i = 1;
            char * p;
            p = &c;
    
          while (fread (p, 1, 1, fhin) ) {
              printf("%c\t%d\n", *p, i++);
          }
          fclose (fhin);
    so basically i'm not sure how represent <FF> in a comparison within the while loop.. any help would be appreciated...


    thanks...

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    i'm dumping a binary (image) file bit by bit - one char per line - using fread. in the terminal i see something like:
    I assume you mean byte by byte, not bit by bit. You should probably be using unsigned char if you're really trying to get 0xFF. Otherwise you may run into some stuff you won't like with sign comparisons. Anyway...
    Code:
    if( byte == 0xFF )
         ...do something...
    else
        ...do something else...

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    <E1> 16
    ^U 17
    You're only seeing <xx> and ^x notation because your terminal is being kind to you.

    If it's binary data, you should use the %x format for printing.
    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.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    18
    thanks... i think i got it from here...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary comparison from 2 files
    By Filly in forum C Programming
    Replies: 6
    Last Post: 01-16-2008, 06:49 AM
  2. Binary comparison
    By tao in forum Windows Programming
    Replies: 0
    Last Post: 06-28-2006, 12:10 PM
  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