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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:01:26+00:00 2026-06-13T20:01:26+00:00

I have the following structure [StructLayout(LayoutKind.Sequential)] public struct SFHeader { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5)]

  • 0

I have the following structure

[StructLayout(LayoutKind.Sequential)]
public struct SFHeader
{

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5)]
    public string FileName;

    public int Offset;

    public short Size;

    public byte Flags;

    public byte Source;


    public long LastWriteTime;


    public byte[] GetBytes()
    {
        int size = Marshal.SizeOf(this);
        var buffer = new byte[size];
        IntPtr ptr = Marshal.AllocHGlobal(size);

        Marshal.StructureToPtr(this, ptr, true);
        Marshal.Copy(ptr, buffer, 0, size);
        Marshal.FreeHGlobal(ptr);

        return buffer;
    }


    public static SFHeader FromBytes(byte[] buffer)
    {
        var str = new SFHeader();
        int size = Marshal.SizeOf(str);

        IntPtr ptr = Marshal.AllocHGlobal(size);
        Marshal.Copy(buffer, 0, ptr, size);
        str = (SFHeader)Marshal.PtrToStructure(ptr, str.GetType());
        Marshal.FreeHGlobal(ptr);

        return str;
    }

}

I need to convert my structure to an array of byte (to send as packet with socket), so I use the GetBytes method, but it returns an array of 24 bytes instead of an array of 21 bytes:

  • Filename (string): 5 bytes
  • Offset (int): 4 bytes
  • Size (short): 2 bytes
  • Flags (byte): 1 byte
  • Source (byte): 1 byte
  • LastWriteTime (long): 8 bytes

So: 5+4+2+1+1+8 = 21 bytes.
This happens because Marshal.SizeOf returns 24, why?
And it seems that the the bytes in excess are placed after the bytes of the string, in fact for example the following structure:

var header = new SFHeader()
{
   FileName = "aaaa",
   Offset = 1,
   Size = 1
};

is converted to the following buffer:

[0] = 97
[1] = 97
[2] = 97
[3] = 97
[4] = 0
[5] = 0
[6] = 0
[7] = 0
[8] = 1
[9] = 0
[10] = 0
[11] = 0
[12] = 1
[13] = 0
... The following are all zero (0)

The fifth, sixth and seventh are the bytes in excess.
How can I solve this problem?

  • 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-13T20:01:27+00:00Added an answer on June 13, 2026 at 8:01 pm

    You’re running into a byte-alignment issue. In an attempt to keep fields on word boundaries for speed of access, the compiler is padding your string with 3 extra bytes. To fix this, use the Pack field of the StructLayoutAttribute.

    [StructLayout(LayoutKind.Sequential, Pack=1)]  // notice the packing here
    public struct SFHeader
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 5)]
        public string FileName;
    
        public int Offset;
    
        public short Size;
    
        public byte Flags;
    
        public byte Source;
    
        public long LastWriteTime;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following structure: [StructLayout(LayoutKind.Sequential)] public struct TCurve { public int fNumItems; /*
I have following data structure (simplified): A giant list of the class TestClass public
I have the following structure: Model public class EventEntry : LogEntry { public EventType
I have following structure typedef struct List_Node { struct File_Descriptor *data; char *key; struct
I have following structure enum quality { good = 0, bad, uncertain }; struct
I have a legacy VB6 application that has the following structure defined: Public Type
Have the following structure [Serializable] public class Parent { public int x = 5;
I have following structure. #include <iostream> #include <fstream> #include <string> #include <sstream> #include <vector>
I have following structure struct { int myData; int myAnotherData; }Value; struct AnotherStructure {
I have following structure: class Employee { public long Id { get; set; }

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.