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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:34:31+00:00 2026-06-06T10:34:31+00:00

Once again I’m receiving structs via UDP from a C++ programm, Now I ported

  • 0

Once again I’m receiving structs via UDP from a C++ programm,
Now I ported the structs to C#, Example:

[Serializable]
struct sample
{
public int in;
public byte[] arr;
public int[] arr2;
public float fl;
}

Ok so how does the Deserializer know when one array ends and the other begins?
Can specify somehow how big the array is?
I don’t want to use fixed, since this makes my code unsafe, and I also can’t use a Constructor since structs are not allowed to contain constructors without parameters.

Any suggestions?

//edit:

the arrays are known to be 32 and 4 long.
the problem is that I don’t know how to pass this information to the deserialiser

then sender is C++ an works like this:

char* pr = &sample;
int i=0;
while (i<sizeof(sample))
{
 udp.send(*(pr+i))
 i++;
}
  • 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-06T10:34:33+00:00Added an answer on June 6, 2026 at 10:34 am

    Now that you have told us that the lengths are of pre-defined length, then the following statement becomes clearer:

    I don’t know how to pass this information to the deserialiser

    In fact, it becomes moot. There is no pre-defined serializer that is going to help you here. You have two options:

    A: write your own serializer, and process the data now that you know the format – perhaps using BinaryReader:

    using(var reader = new BinaryReader(source)) {
    
        int in = reader.ReadInt32();
        byte[] arr = reader.ReadBytes(32);
        int[] arr2 = new int[4];
        for(int i = 0 ; i < 4 ; i++) arr2[i] = reader.ReadInt32();
        float fl = reader.ReadSingle();
    
        var obj = /* something involving ^^^ */
    }
    

    B: buffer 56 bytes, and use really nasty unsafe / fixed / pointer-banging code

    I strongly suggest the first. In particular, this will also allow you to address endianness if required.

    IN THE NAME OF EVERYTHING SACRED TO YOU, DO NOT DO THIS:

    using System;
    using System.Runtime.InteropServices;
    [StructLayout(LayoutKind.Explicit)]
    unsafe struct sample
    {
        [FieldOffset(0)] public int @in;
        [FieldOffset(4)] public fixed byte arr[32];
        [FieldOffset(36)] public fixed int arr2[4];
        [FieldOffset(52)] public float fl;    
    }
    
    static class Program
    {
        unsafe static void Main()
        {
            byte[] buffer = new byte[56];
            new Random().NextBytes(buffer); // some data...
    
            sample result;
            fixed(byte* tmp = buffer)
            {
                sample* ptr = (sample*) tmp;
                result = ptr[0];
            }
    
            Console.WriteLine(result.@in);
            Console.WriteLine(result.fl);
        }    
    }
    

    For larger buffers, you can treat ptr as an unsafe array of multiple sample, accessed by index:

    int @in = ptr[i].@in;
    

    (etc)

    But honestly… there are so many things “evil” with that, I honestly don’t know where to begin… just… unless you know absolutely what every line in there is doing, have done it before, and understand all the traps… DON’T EVEN THINK ABOUT IT

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

Sidebar

Related Questions

Once again, I wish C++ had stronger typedef s: #include <vector> template<typename T> struct
Hi good people once again While busy traversing, iterating and manipulating my arrays from
I'm looking once again for help from the collective. I created a site which
Once again I need some much appreciated help from the experts :) I am
once again I ask for help. I haven't coded anything for sometime! Now I
Once again, I'm at a loss with IE7 on this one. I'm making a
Once again, lists are boggling my mind. <div id=headbottom> <ul id=headnavbutton> <li id=button1 class=headnavbutton></li>
Once again my silliness has struck. I would like to thank everyone who helped
Once again a question about the garbage collector in actionscript-3: If I have a
Once again I'm doing Java graphics (Graphics2D) but I noticed there is no Polygon.Double

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.