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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:04:23+00:00 2026-05-11T11:04:23+00:00

What is the best way to get a byte array from a struct to

  • 0

What is the best way to get a byte array from a struct to send over TCP sockets? I’m using .Net (VB or C#).

  • 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. 2026-05-11T11:04:23+00:00Added an answer on May 11, 2026 at 11:04 am

    One option is to marshal the native representation of the struct into a buffer directly, similar to how memcpy works in C.

    You would need to add the appropriate attribute to your struct,

      [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack=1)] 

    Then you can serialize it using:

        /// <summary>     /// Serializes the specified object into a byte array.     /// </summary>     /// <param name='nativeObject'>The object to serialize.</param>     /// <returns></returns>     public static byte[] Serialize(object obj)     {         Type objectType = obj.GetType();         int objectSize = Marshal.SizeOf(obj);         IntPtr buffer = Marshal.AllocHGlobal(objectSize);         Marshal.StructureToPtr(obj, buffer, false);         byte[] array = new byte[objectSize];         Marshal.Copy(buffer, array , 0, objectSize);         Marshal.FreeHGlobal(buffer);         return array;     } 

    Still, this is by far the least portable solution. Both sides will need to use the same alignment and endianness, and you will need to implement versioning yourself if you will ever need to change the struct itself.

    In most cases, your serialization format shouldn’t be a direct copy of your internal data structures.

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

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The mechanism for I/O is implementation dependent. In addition, there… May 11, 2026 at 11:41 pm
  • Editorial Team
    Editorial Team added an answer Most sites, especially ones that retain login information, send confirmation… May 11, 2026 at 11:41 pm
  • Editorial Team
    Editorial Team added an answer PHP does 'copy on write' anyway, so variables aren't actually… May 11, 2026 at 11:41 pm

Related Questions

Im trying to work out the best way scale my site, and i have
I'm looking for some suggestions on how to go about this. Any input is
What is the best way to scale a 2D image array? For instance, suppose
I have a byte array that I am reading in from a NetworkStream. The

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.