Re: unpacking ints

From: Karl Heinz Buchegger (kbuchegg_at_gascad.at)
Date: 03/02/04


Date: Tue, 02 Mar 2004 14:19:21 +0100

Alberto Barbati wrote:
>
> Karl Heinz Buchegger wrote:
> >>Is my method above for unpacking the int correct?
> >
> > The casts are wrong. You need to cast the individual bytes to unsigned int
> > first and cast the total result to int only after the number is constructed.
>
> Wrong. You need to cast the individual bytes to unsigned char not
> unsigned int. Otherwise a signed char will be promoted to (signed) int
> before being converted to unsigned int.

Right.
I assumed that anybody working at the bytelevel uses unsigned char
to store bytes and not plain char.

-- 
Karl Heinz Buchegger
kbuchegg@gascad.at


Relevant Pages

  • Re: Question about cast
    ... the cast specifies a conversion even if the type ... machine unsigned int is 32 bits and unsigned char is 8 ... If noone here knows do you think it is worthwhile to ...
    (comp.lang.c)
  • Re: Question about cast
    ... the cast specifies a conversion even if the type ... machine unsigned int is 32 bits and unsigned char is 8 ... A cast is simply an operator that specifies a conversion. ... document belongs in comp.std.c; discussion of the language it defines ...
    (comp.lang.c)
  • Re: size of a struct
    ... "an unsigned integer" might not be an unsigned int. ... So in C99 we would have to cast it to unsigned long long? ... If your runtime library is C99 compliant (which it might not be even ...
    (comp.lang.c)
  • Question about cast
    ... with greater precision or range than required ... the cast specifies a conversion even if the type ... machine unsigned int is 32 bits and unsigned char is 8 ...
    (comp.lang.c)
  • Re: cast vs. suffixes
    ... Either you use an explicit cast, as in: (unsigned int) 1234 or you use the UL suffixes, as in: ... Since the preprocessor operates at an early stage of compilation before types "exist," it cannot evaluate cast operators -- since casts are all about type conversions and there aren't any types yet, there's really nothing the preprocessor can do with them. ...
    (comp.lang.c)