0

I have a typedef struct with different data types in it. The number array has negative and non-negative values. How do I convert this struct in to a unint8t array in C++ on the Linux platform. Appreciate some help on this. Thank you. The reason I am trying to do the conversation is to send this uint8_t buffer as a parameter to a function.

typedef struct
{
  int enable;
  char name;
  int numbers[5];
  float counter;
};

appreciate any example on doing this. thank you

6
  • 1
    By the way, why are you using typedef for your structure if you're programming in C++? That's not needed. Commented Nov 10, 2022 at 12:54
  • Oh, and a name with only a single letter? That won't even be able to hold a C-style null-terminated string, except the empty string (which have only the null-terminator). Commented Nov 10, 2022 at 13:00
  • What are you trying to solve, sending data over a network, save/load objects to disk? You are entering the territory of binary serialization. Don't take anything for granted (and that includes the memory layout of your struct). And also don't assume it is always safe to cast that array back to a valid object (POD's should be fine) Commented Nov 10, 2022 at 13:04
  • 2
    In that case simply casting is fine - but your struct layout will have platform-specific endianness and padding which are not, in general, guaranteed to be the same as the recipient's. Commented Nov 10, 2022 at 13:31
  • 3
    Reinforcing Useless's comment, my project was "burned" by not properly serializing/deserializing struct objects that were going out over TCP/IP connections when our platform moved from DEC Alpha Tru64 UNIX to a mixed environment with Intel-based Windows NT machines as well. Padding changed, endian-ness changed, and even sizeof some primitive types changed. It was a mess. Commented Nov 10, 2022 at 13:44

2 Answers 2

0

For a plain old data structure like the one you show this is trivial:

You know the size of the structure in bytes (from the sizeof operator).

That means you can create a vector of bytes of that size.

Once you have that vector you can copy the bytes of the structure object into the vector.

Now you have, essentially, an array of bytes representation of the structure object.


In code it would be something like this:

struct my_struct_type
{
    int enable;
    char name;
    int numbers[5];
    float counter;
};

my_struct_type my_struct_object = {
    // TODO: Some valid initialization here...
};

// Depending on the compiler you're using, and its C++ standard used
// you might need to use `std::uint8_t` instead of `std::byte`
std::vector<std::byte> bytes(sizeof my_struct_object);

std::memcpy(bytes.data(), reinterpret_cast<void*>(&my_struct_object), sizeof my_struct_object);
Sign up to request clarification or add additional context in comments.

1 Comment

yes I know the size of the struct. How do I copy the bytes from the structure into the vector?
0

Suggestion: use char*. For that type, C++ allows you to cast any pointer to it and use reinterpret_cast<char*>(&obj). (Assuming Obj obj;.)

If you need to use uint8_t* (and it's not a typedef to char*, you can allocate sufficient memory and do memcpy:

uint8_t buffer[sizeof(Obj)];
memcpy(buffer, &obj, sizeof(obj));

2 Comments

so once the bufffer is sent over the tcp socket how do I construct the struct on the server side. Appreciate an example
@jkafernando Same way, you just swap dest and src: data is in buffer, then memcpy(&obj, buffer, sizeof(obj));.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.