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

  • Home
  • SEARCH
  • 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 9093583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:08:21+00:00 2026-06-16T23:08:21+00:00

Hi i have a structure as follows private struct MessageFormat { public byte[] Header;

  • 0

Hi i have a structure as follows

    private struct MessageFormat
    {
        public byte[] Header; //start of message
        public byte Fragmentation; //single packet or not
        public byte Encryption; //encrypted message
        public byte[] Authentication; //password
        public byte[] Flag; //to locate end of auth
        public byte[] Data; //info
        public byte[] Trailer; //end of message
    }

is there a handy way to convert the whole MessageFormat into a single byte array[] after i populate all the fields?

  • 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-16T23:08:23+00:00Added an answer on June 16, 2026 at 11:08 pm

    I’ve written a sample code which will do exactly what you asked. It combines Reflection and Buffer.BlockCopy but it can be made MUCH more performant (i.e. boxing format in advance, avoiding anonymous types — and their awkward, redundant initialization in the example — or even not using Reflection at all and hardcoding the serialization method). Note that the method I offer doesn’t use a buffer for the result but calculates the final length before allocating the array.

    var format = new MessageFormat
    {
        //Initialize your format
    };
    //Gets every instance public field in the type
    //Expects it to be either byte, either byte[]
    //Extracts value, length and type
    var fields = typeof (MessageFormat).GetFields().
        Select(f => f.GetValue(format)).
        Select(v => new
        {
            Value = v,
            Type = v is byte ? typeof (byte) : typeof (byte[]),
            Length = v is byte ? 1 : (v as byte[]).Length
        }).
        ToArray();
    //Calculates the resulting array's length
    var totalLength = fields.Sum(v => v.Length);
    var bytes = new byte[totalLength];
    var writingIndex = 0;
    foreach (var field in fields)
    {
        //If the field is a byte, write at current index,
        //then increment the index
        if (field.Type == typeof (byte))
            bytes[writingIndex++] = (byte) field.Value;
        else
        {
            //Otherwise, use a performant memory copy method
            var source = field.Value as byte[];
            var sourceLength = source.Length;
            Buffer.BlockCopy(source, 0, bytes, writingIndex, sourceLength);
            writingIndex += sourceLength;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class with a structure as follows: class Test { private $var1;
I have the enum structure as follows: public enum MyEnum { One=1, Two=2, Three=3
For a tree structure as follows public class Node implements Comparable<Node> { private List<Node>
I have code that follows this structure: class someClass { private static $pay =
If I have class structure as follows: private class A { int afoo {
I have a structure represented as follows: (Example) struct struct3 { struct structchild4 {
I have a folder structure as follows: mydomain.example ->Folder-A ->Folder-B I have a string
I have a directory structure as follows Client_Site/ some_folder1/ some_folder2/ some_folder3/ Lots of files
I have a python data-structure as follows: A = [{'abc': 'kjkjl'},{'abc': 'hjhjh'},{'abc': '78787'}] How
I have a table and the structure of it is as follows: VoteId QuestionId

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.