Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Auto-byte-swapping that can be used in a union [modified]

Auto-byte-swapping that can be used in a union [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    ClementsDan
    wrote on last edited by
    #1

    We've got a bunch of apps that need to prepare data for a machine that uses big-endian byte order. So we have a bunch of structs that look like:

    struct tagFOO_COMREG
    {
    UINT16 ui16DataLength_BE;
    UINT32 ui32SeqNum_BE;
    // ...
    } FOO_COMREG_s, *PFOO_COMREG_s;

    where the _BE is a reminder that these values have to be byte-swapped. So, one day I thought it would be convenient to be able to assign to one of the struct members without having to explicitly do the byte swapping. So I wrote some classes like:

    class UINT16_BE
    {
    private:
    BYTE m_abData[2];
    public:
    UINT16_BE(void)
    {
    // do nothing
    }
    UINT16_BE(UINT16 ui16)
    {
    m_abData[0] = static_cast<BYTE>(ui16 >> 8);
    m_abData[1] = static_cast<BYTE>(ui16);
    }
    operator UINT16(void)
    {
    return ((m_abData[0] << 8) | m_abData[1]);
    }
    };

    And it works fine. Until my co-worker comes up to me asking how to fix a compiler error he's having. He's trying to put these in a union, which doesn't work because the constructors keep it from being recognized as POD (despite the fact that the class was designed to be memcpy-able). So, is there a way around this?

    modified on Monday, February 11, 2008 2:13 PM

    M 1 Reply Last reply
    0
    • C ClementsDan

      We've got a bunch of apps that need to prepare data for a machine that uses big-endian byte order. So we have a bunch of structs that look like:

      struct tagFOO_COMREG
      {
      UINT16 ui16DataLength_BE;
      UINT32 ui32SeqNum_BE;
      // ...
      } FOO_COMREG_s, *PFOO_COMREG_s;

      where the _BE is a reminder that these values have to be byte-swapped. So, one day I thought it would be convenient to be able to assign to one of the struct members without having to explicitly do the byte swapping. So I wrote some classes like:

      class UINT16_BE
      {
      private:
      BYTE m_abData[2];
      public:
      UINT16_BE(void)
      {
      // do nothing
      }
      UINT16_BE(UINT16 ui16)
      {
      m_abData[0] = static_cast<BYTE>(ui16 >> 8);
      m_abData[1] = static_cast<BYTE>(ui16);
      }
      operator UINT16(void)
      {
      return ((m_abData[0] << 8) | m_abData[1]);
      }
      };

      And it works fine. Until my co-worker comes up to me asking how to fix a compiler error he's having. He's trying to put these in a union, which doesn't work because the constructors keep it from being recognized as POD (despite the fact that the class was designed to be memcpy-able). So, is there a way around this?

      modified on Monday, February 11, 2008 2:13 PM

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      ClementsDan wrote:

      He's trying to put these in a union

      The constructor's not going to work there...maybe something like this will work...

      class UINT16_BE
      {
      private:
      BYTE m_abData[2];
      public:
      UINT16_BE &operator =(UINT16 ui16)
      {
      m_abData[0] = static_cast(ui16 >> 8);
      m_abData[1] = static_cast(ui16);
      return *this;
      }
      operator UINT16() const
      {
      return ((m_abData[0] << 8) | m_abData[1]);
      }
      };

      Mark


      Last modified: 22mins after originally posted --

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      L 1 Reply Last reply
      0
      • M Mark Salsbery

        ClementsDan wrote:

        He's trying to put these in a union

        The constructor's not going to work there...maybe something like this will work...

        class UINT16_BE
        {
        private:
        BYTE m_abData[2];
        public:
        UINT16_BE &operator =(UINT16 ui16)
        {
        m_abData[0] = static_cast(ui16 >> 8);
        m_abData[1] = static_cast(ui16);
        return *this;
        }
        operator UINT16() const
        {
        return ((m_abData[0] << 8) | m_abData[1]);
        }
        };

        Mark


        Last modified: 22mins after originally posted --

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #3

        operator UINT16(void) const ;P

        led mike

        M 1 Reply Last reply
        0
        • L led mike

          operator UINT16(void) const ;P

          led mike

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Fixed! ;P

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups