Thread: binary comparison

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

    binary comparison

    hi all...

    i have a small program that reads an image char by char in binary mode. so the output looks like :

    <FF> 1
    <D8> 2
    <FF> 3
    <E1> 4
    ^W 5
    { 6
    E 7
    x 8
    i 9
    f 10
    ^@ 11
    ^@ 12
    M 13
    M 14
    ^@ 15
    * 16
    ........

    i need to make sure that the first char - here above as <FF> - is always at the start of the file. sometimes it isn't. so i need to look for the <FF>. in order to find it i was told to look for 0xFF. but when i do the comparison as in (c == 0xFF ) is not doing it.

    here is what i got:
    Code:
    int main (int argc, char *argv [])
        {
          FILE * fhin = fopen (argv[1], "rb");
            char c;
            int i = 1;
    
          while (fread (&c, 1, 1, fhin) ) {
            if ( c == 0xFF) printf("%s\t", "moo");
             printf("%c\t%d\n", c, i++);
          }
         
          fclose (fhin);
    
    
          return (0);
        }

    what would be the correct way?

    thanks..

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Signed chars have a sign bit, which is either on when the byte, interpreted as a number, is negative. When the sign bit doesn't matter you should be using unsigned chars. Of course, all signed chars fit in an unsigned char, so that is not a problem to worry about, either.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The problem here is that char can be either signed or unsigned. It's implementation specific. If you need an unsigned value, then use an unsigned type.


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

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    18
    Quote Originally Posted by quzah View Post
    The problem here is that char can be either signed or unsigned. It's implementation specific. If you need an unsigned value, then use an unsigned type.


    Quzah.
    got it. thanks...

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Just make sure that the things on both sides of the equals sign are the same type.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. binary comparison
    By toshog in forum C Programming
    Replies: 3
    Last Post: 05-03-2010, 11:37 PM
  2. Binary comparison from 2 files
    By Filly in forum C Programming
    Replies: 6
    Last Post: 01-16-2008, 06:49 AM
  3. Binary comparison
    By tao in forum Windows Programming
    Replies: 0
    Last Post: 06-28-2006, 12:10 PM
  4. 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
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM