Re: FAQ Related - why cast?

From: Martin (martin.o_brien_at_[no-spam)
Date: 01/05/05


Date: Wed, 05 Jan 2005 17:13:34 GMT


"Dietmar Schindler" <dSpam@arcor.de> wrote in message
news:41DBBCB6.52BA@arcor.de...
> Provided that you stated the FAQ answer correctly, the explanation is
> nonsense (the left hand side of the assignment expression is of type
> int, and without the cast, the right hand side is also of type int; so
> there is no extension).

To ensure the partial quote I gave in my initial post was not misleading,
this is the question and answer from the book (c)1996 by Addison-Wesley
Publishing Company, Inc.

Question: How can I write code to conform to these old, binary data file
formats?

Answer: It's difficult because of word size and byte-order differences,
floating-point formats, and structure padding. To get the control you need
over these particulars, you may have to read and write things a byte at a
time, shuffling and rearranging as you go. (This isn't always as bad as it
sounds and gives you both code portability and complete
control.) For example, suppose that you want to read a data structure,
consisting of a character, a 32-bit integer, and a 16-bit integer, from the
stream fp into the C structure

   struct mystruct {
      char c;
      long int i32;
      int i16;
   };

You might use code like this:

      s.c = getc(fp);

      s.i32 = (long)getc(fp) << 24;
      s.i32 |= (long)getc(fp) << 16;
      s.i32 |= (unsigned)(getc(fp) << 8);
      s.i32 |= getc(fp);

      s.i16 = getc(fp) << 8;
      s.i16 |= getc(fp);

This code assumes that getc reads 8-bit characters and that the data is
stored most significant byte first ("big endian"). The casts to (long)
ensure that the 16- and 24-bit shifts operate on long values (see question
3.14), and the cast to (unsigned) guards against sign extension. (In
general, it's safer to use all unsigned types when writing code like this,
but see question 3.19.)

The corresponding code to write the structure might look like:

      putc(s.c, fp);
      putc((unsigned)((s.i32 >> 24) & 0xff), fp);
      putc((unsigned)((s.i32 >> 16) & 0xff), fp);
      putc((unsigned)((s.i32 >> 8) & 0xff), fp);
      putc((unsigned)(s.i32 & 0xff), fp);
      putc(s.i16 >> 8) & 0xff, fp);
      putc(s.i16 & 0xff, fp);

See also questions 2.12, 12.38, 16.7, and 20.5.

-- 
Martin
http://martinobrien.co.uk/ 


Relevant Pages

  • Re: CButton casts
    ... >The pointer returned is usually cast to the type of control identified by ... >Microsfot's cast technique. ... pointer to the CWnd part of that object, which you can safely cast to the ...
    (microsoft.public.vc.mfc)
  • Re: Development news catch-up
    ... to topline a new original movie for the cable channel, ... Does Ponce have the chops to be the lead/recurring character? ... 10-to-1 says this means a new cast. ... "Summerland") is the latest addition to the comedy pilot, ...
    (rec.arts.tv)
  • Re: The importance of Kid Vulcan as a new X-Man?
    ... successful addition to the X-Men cast, primarily as a new X-Man. ... character will be the one to break this rut. ... Will it be Kid Vulcan? ... He's set to be the driver of the first Brubaker written Uncanny X-Men ...
    (rec.arts.comics.marvel.xbooks)
  • Re: The importance of Kid Vulcan as a new X-Man?
    ... successful addition to the X-Men cast, primarily as a new X-Man. ... character will be the one to break this rut. ... Will it be Kid Vulcan? ... He's set to be the driver of the first Brubaker written Uncanny X-Men ...
    (rec.arts.comics.marvel.xbooks)
  • Re: The importance of Kid Vulcan as a new X-Man?
    ... successful addition to the X-Men cast, primarily as a new X-Man. ... character will be the one to break this rut. ... Will it be Kid Vulcan? ... he's going to be a member of the X-Men. ...
    (rec.arts.comics.marvel.xbooks)