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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:32:07+00:00 2026-05-10T19:32:07+00:00

Given a FieldInfo object and an object, I need to get the actual bytes

  • 0

Given a FieldInfo object and an object, I need to get the actual bytes representation of the field. I know that the field is either int,Int32,uint,short etc.

How can I get the actual byte representation? BinaryFormatter.Serialize won’t help, since it’ll give me more information than I need (it also records type name etc.). The Marshal class does not seem to have facilities to use bytes array (but maybe I’m missing something).

Thanks

  • 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-10T19:32:08+00:00Added an answer on May 10, 2026 at 7:32 pm

    You may also try code like the following if what you actually want is to transfer structures as a byte array:

    int rawsize = Marshal.SizeOf(value); byte[] rawdata = new byte[rawsize]; GCHandle handle = GCHandle.Alloc(rawdata, GCHandleType.Pinned); Marshal.StructureToPtr(value, handle.AddrOfPinnedObject(), false); handle.Free(); 

    This converts the given object value to the byte array rawdata. I’ve taken this from code I previously wrote, and you may need to adapt it to your needs to make it actually work. I used it for communication with some hardware with user-defined structures, but it should work for built-in types as well (after all, they’re structures, aren’t they?)

    To make structure members properly aligned, use the StructLayout attribute to specify one-byte-alignment:

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

    And then use the MarshalAs attribute as needed for fields, e.g. for inline arrays:

    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] byte[] _state; 

    The code to get the structure back from the byte array is something like this:

    public T GetValue<T>() {     GCHandle handle = GCHandle.Alloc(RawValue, GCHandleType.Pinned);     T structure = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),                        typeof(T));     handle.Free();     return structure; } 

    Of course you’ll need to know the type you want for this to work.

    Note that this will not handle endianness for itself. In my project, most fields were one byte only, so it didn’t matter, but for the few fields where it did, I just made the fields private and added public properties that would take care of the endianness (Jon Skeet’s link from a comment to his answer may help you, I wrote some utility functions for this since I needed only few).

    When I needed this, I created a Message class that would store the raw value (hence the GetValue method, the code at the top is actually the body of a SetValue method) and had some nice convenience method to get the value formatted etc.

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

Sidebar

Ask A Question

Stats

  • Questions 162k
  • Answers 162k
  • 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 It works for my Firefox 2, Chrome 2. Here is… May 12, 2026 at 11:56 am
  • Editorial Team
    Editorial Team added an answer So you're just after straightforward linear interpolation, right? As in… May 12, 2026 at 11:56 am
  • Editorial Team
    Editorial Team added an answer Silly me, I started tomcat with tomcat6.exe instead of startup.bat.… May 12, 2026 at 11:56 am

Related Questions

At the moment my code successfully sets the value of fields/properties/arrays of an object
How do I check if a given object is nullable in other words how
I'm writing a custom deserializer that will deserialize a list by deserializing each of
I've got a set of static enumeration classes that I'm using to hold meaningful

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.