Boost logo

Boost Users :

From: Jim.Hyslop (jim.hyslop_at_[hidden])
Date: 2003-07-09 09:51:14


Vladimir Prus wrote:
> Sorry, I don't understand what you're saying...
>
> std::cout << "Optimal CRC-8 : " << std::hex << (unsigned)crc_8()
> << std::endl;
>
> is the same as
>
> std::cout << "Optimal CRC-8 : " << std::hex << (unsigned int)crc_8()
> << std::endl;
Sorry, I wasn't very clear in my last message.

The results can be unexpected, depending on whether the result of crc_8()
(or more precisely, operator() ) is signed or unsigned. If operator()
returns an unsigned value, there will be no problems, and the output will be
ea.

On the other hand, if operator() returns a signed value, then it will first
be promoted to a signed int, then converted to an unsigned int. Using the
original example values posted by Victoria, the output would be ffffffea. In
this scenario, the return value must be first cast to an unsigned char, then
to an unsigned int:
  ... << static_cast<unsigned>( static_cast<unsigned char>( crc_8() ) )

I just had a quick look at the crc.hpp file, and operator() returns an
unsigned value, thus banishing my caveat to the dusty corners where all
academic footnotes end up ;-)

-- 
Jim

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net