Boost logo

Boost Users :

Subject: [Boost-users] reinterpret_cast boost integer
From: Brad Tilley (kj4eit_at_[hidden])
Date: 2012-02-04 22:18:11


I've been experimenting with reinterpret_cast and boost integer. I've
found that I can correctly cast uint16_t, uint32_t and uint64_t,
however, when I try to do that with a uint8_t or int8_t (both one
byte), it doesn't seem to work. Below is some sample code that
demonstrates the issue.

This is not really boost specific, it's probably just my
misunderstanding/misuse of reinterpret_cast, but if anyone here could
point me in the right direction, I'd appreciate it.

Thanks,

Brad

#include <iostream>
#include <boost/integer.hpp>

void process_uint16_t( const std::string& bytes )
{
    const boost::uint16_t * data_ptr;
    boost::uint16_t data;

    data_ptr = reinterpret_cast<const boost::uint16_t*>(bytes.data());
    data = *data_ptr;

    std::cout << data << std::endl;
}

void process_uint8_t( const std::string& bytes )
{
    const boost::uint8_t * data_ptr;
    boost::uint8_t data;

    data_ptr = reinterpret_cast<const boost::uint8_t*>(bytes.data());
    data = *data_ptr;

    std::cout << data << std::endl;
}

int main()
{
    std::string r1("\u001F\u001F");
    std::string r("\u001F");

    process_uint16_t(r1);
    process_uint8_t(r);

    return 0;
}


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