Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8737777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:35:56+00:00 2026-06-13T10:35:56+00:00

The application I am working on receives C style structs from an embed system

  • 0

The application I am working on receives C style structs from an embed system whose code was generated to target a 16 bit processor. The application which speaks with the embedded system is built with either a 32 bit gcc compiler, or a 32 bit MSVC c++ compiler. The communication between the application and the embedded system takes place via UDP packets over ethernet or modem.

The payload within the UDP packets consist of various different C style structs. On the application side a C++ style reinterpret_cast is capable of taking the unsigned byte array and casting it into the appropriate struct.

However, I run into problems with reinterpret_cast when the struct contains enumerated values. The 16 bit Watcom compiler will treat enumerated values as an uint8_t type. However, on the application side the enumerated values are treated as 32 bit values. When I receive a packet with enumerated values in it the data gets garbled because the size of the struct on the application side is larger the struct on the embedded side.

The solution to this problem, so far, has been to change the enumerated type within the struct on the application side to an uint8_t. However, this is not an optimal solution because we can no longer use the member as an enumerated type.

What I am looking for is a solution which will allow me to use a simple cast operation without having to tamper with the struct definition in the source on the application side. By doing so, I can use the struct as is in the upper layers of my application.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:35:57+00:00Added an answer on June 13, 2026 at 10:35 am

    As noted, correctly deal with the issue is proper serialization and deserialization.

    But it doesn’t mean we can’t try some hacks.

    Option 1:
    If you particular compiler support packing the enum (in my case gcc 4.7 in windows), this might work:

    typedef enum { VALUE_1 = 1, VALUE_2, VALUE_3 }__attribute__ ((__packed__)) TheRealEnum;
    

    Option 2:

    If your particular compiler supports class sizes of < 4 bytes, you can use a HackedEnum class which uses operator overloading for the conversion (note the gcc attribute you might not want it):

    class HackedEnum
    {
    private:
        uint8_t evalue;
    public:
        void operator=(const TheRealEnum v) { evalue = v; };
        operator TheRealEnum() { return (TheRealEnum)evalue; };
    }__attribute__((packed));
    

    You would replace TheRealEnum in your structures for HackedEnum, but you still continue using it as TheRealEnum.

    A full example to see it working:

    #include <iostream>
    #include <stddef.h>
    
    using namespace std;
    
    #pragma pack(push, 1)
    
    typedef enum { VALUE_1 = 1, VALUE_2, VALUE_3 } TheRealEnum;
    
    typedef struct
    {
        uint16_t v1;
        uint8_t enumValue;
        uint16_t v2;
    }__attribute__((packed)) ShortStruct;
    
    typedef struct
    {
        uint16_t v1;
        TheRealEnum enumValue;
        uint16_t v2;
    }__attribute__((packed)) LongStruct;
    
    class HackedEnum
    {
    private:
        uint8_t evalue;
    public:
        void operator=(const TheRealEnum v) { evalue = v; };
        operator TheRealEnum() { return (TheRealEnum)evalue; };
    }__attribute__((packed));
    
    typedef struct
    {
        uint16_t v1;
        HackedEnum enumValue;
        uint16_t v2;
    }__attribute__((packed)) HackedStruct;
    
    #pragma pop()
    
    int main(int argc, char **argv)
    {
        cout << "Sizes: " << endl
             << "TheRealEnum: " << sizeof(TheRealEnum) << endl
             << "ShortStruct: " << sizeof(ShortStruct) << endl
             << "LongStruct: " << sizeof(LongStruct) << endl
             << "HackedStruct: " << sizeof(HackedStruct) << endl;
    
        ShortStruct ss;
        cout << "address of ss: " << &ss <<  " size " << sizeof(ss) <<endl
             << "address of ss.v1: " << (void*)&ss.v1 << endl
             << "address of ss.ev: " << (void*)&ss.enumValue << endl
             << "address of ss.v2: " << (void*)&ss.v2 << endl;
    
        LongStruct ls;
        cout << "address of ls: " << &ls <<  " size " << sizeof(ls) <<endl
             << "address of ls.v1: " << (void*)&ls.v1 << endl
             << "address of ls.ev: " << (void*)&ls.enumValue << endl
             << "address of ls.v2: " << (void*)&ls.v2 << endl;
    
        HackedStruct hs;
        cout << "address of hs: " << &hs <<  " size " << sizeof(hs) <<endl
             << "address of hs.v1: " << (void*)&hs.v1 << endl
             << "address of hs.ev: " << (void*)&hs.enumValue << endl
             << "address of hs.v2: " << (void*)&hs.v2 << endl;
    
    
        uint8_t buffer[512] = {0};
    
        ShortStruct * short_ptr = (ShortStruct*)buffer;
        LongStruct * long_ptr = (LongStruct*)buffer;
        HackedStruct * hacked_ptr = (HackedStruct*)buffer;
    
        short_ptr->v1 = 1;
        short_ptr->enumValue = VALUE_2;
        short_ptr->v2 = 3;
    
        cout << "Values of short: " << endl
                << "v1 = " << short_ptr->v1 << endl
                << "ev = " << (int)short_ptr->enumValue << endl
                << "v2 = " << short_ptr->v2 << endl;
    
        cout << "Values of long: " << endl
                << "v1 = " << long_ptr->v1 << endl
                << "ev = " << long_ptr->enumValue << endl
                << "v2 = " << long_ptr->v2 << endl;
    
        cout << "Values of hacked: " << endl
                << "v1 = " << hacked_ptr->v1 << endl
                << "ev = " << hacked_ptr->enumValue << endl
                << "v2 = " << hacked_ptr->v2 << endl;
    
    
    
        HackedStruct hs1, hs2;
    
        // hs1.enumValue = 1; // error, the value is not the wanted enum
    
        hs1.enumValue = VALUE_1;
        int a = hs1.enumValue;
        TheRealEnum b = hs1.enumValue;
        hs2.enumValue = hs1.enumValue;
    
        return 0;
    }
    

    The output on my particular system is:

    Sizes:
    TheRealEnum: 4
    ShortStruct: 5
    LongStruct: 8
    HackedStruct: 5
    address of ss: 0x22ff17 size 5
    address of ss.v1: 0x22ff17
    address of ss.ev: 0x22ff19
    address of ss.v2: 0x22ff1a
    address of ls: 0x22ff0f size 8
    address of ls.v1: 0x22ff0f
    address of ls.ev: 0x22ff11
    address of ls.v2: 0x22ff15
    address of hs: 0x22ff0a size 5
    address of hs.v1: 0x22ff0a
    address of hs.ev: 0x22ff0c
    address of hs.v2: 0x22ff0d
    Values of short:
    v1 = 1
    ev = 2
    v2 = 3
    Values of long:
    v1 = 1
    ev = 770
    v2 = 0
    Values of hacked:
    v1 = 1
    ev = 2
    v2 = 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a data distribution application which receives data from a source and
Presently I am working on an application which sends and receives file from remote
Am currently working on a web application which receives the encoded text from the
I am working on a server application which receives data over a TCP socket
My Application was working fine. But as soon as i updated from ADT 16
I have an application which periodically receives files and saves them in the internal
I'm working on a java project that receives midi events from midi hardware using
Whenever my server application receives a packet which is too big for the buffer,
I am working on an iPhone project containing a ton of code. The application
I have a working piece of code in the application delegate that contains a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.