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 8744463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:50:39+00:00 2026-06-13T11:50:39+00:00

I am an absolute beginner to C++ and Qt. I am broadcasting different data

  • 0

I am an absolute beginner to C++ and Qt.

I am broadcasting different data via TCP using Qt. The process of sending and retrieving data works fine but I have problems on interpreting the data on the receivers side.

The data is represented in different structs which have in common that they have a commandId and a state. The rest could be anything like an error message, a filename or something else. This code is not written on my own and I am not allowed to change it (e.g: define and implement a common interfaces.)

typedef struct
{
  uint8_t commandId;
  State state;
  //special data
  QString errorMessage;
} Command1;

typedef struct
{
  uint8_t commandId;
  State state;
  //special data
  uint8_t amountSensors;
} Command2;

enum State {
  STATID_PAUSE = 50000
  STATID_RECORD = 50001
  STATID_PLAY = 50002
  STATID_ERROR = 50003
}

The sender is converting a struct into a QByteArray this way:

Command1 example;
example.commandId = 134;
example.state = STATID_ERROR;

char *p_Char;
p_char = reinterpret_cast<char*>(&example);
QByteArray qba(p_char, sizeof(p_char));

Now I have to write a receiver, but the receiver doesn’t know what he gets (Command1, Command2 or something else). He would be able to interpret if he could read out the commandId and the state.

At this moment I am able to read out the commandId like this:

commandId = static_cast<uint8_t>(qba[0]);

but how could I read out the State which is an enum?

  • 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-13T11:50:40+00:00Added an answer on June 13, 2026 at 11:50 am

    State values will take the size of an int. which means to access it you will do :

     State state = (State) (*( reinterpret_cast<const int*>(qba.constData()+1)) );
    

    First you reinterpret the const char pointer as an const int pointer, then you deference it (which means you obtain the value), and you cast this value as an State.

    In order to access other variables you will start at index 1 + sizeof(int) = 1+ sizeof(State)

    See this thread about the size of an enum.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am absolute beginner to drupal. I have added a contact form (using Webform
Absolute beginner question: I have a template file index.html that looks like this: ...
I'm almost an absolute beginner in Python, but I am asked to manage some
I have an absolute beginner question here. I'm having trouble getting multiple checkbox entries
I want to develop a website with java but I'm absolute beginner in java
I'm an absolute beginner using IDLE (Python 2.6.4) to learn the basics. I recently
I'm an absolute beginner in Outlook programming and Windows GUI programming in general. But
I am an absolute beginner in this field. I have written the following code
I'm an absolute beginner when it comes to using both SWIG and lua, and
I am new to Javascript and development in general and have an absolute beginner's

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.