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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:18:47+00:00 2026-05-14T00:18:47+00:00

Say I have a collection of bytes var bytes = new byte[] {0, 1,

  • 0

Say I have a collection of bytes

var bytes = new byte[] {0, 1, 2, 3, 4, 5, 6, 7};

and I want to pull out a defined value from the bytes as a managed type, e.g. a ushort. What is a simple way to define what types reside at what location in the collection and pull out those values?

One (ugly) way is to use System.BitConverter and a Queue or byte[] with an index and simply iterate through, e.g.:

int index = 0;
ushort first = System.BitConverter.ToUint16(bytes, index);
index += 2; // size of a ushort
int second = System.BitConverter.ToInt32(bytes, index);
index += 4;
...

This method gets very, very tedious when you deal with a lot of these structures!

I know that there is the System.Runtime.InteropServices.StructLayoutAttribute which allows me to define the locations of types inside a struct or class, but there doesn’t seem to be a way to import the collection of bytes into that struct. If I could somehow overlay the struct on the collection of bytes and pull out the values, that would be ideal. E.g.

Foo foo = (Foo)bytes; // doesn't work because I'd need to implement the implicit operator
ushort first = foo.first;
int second = foo.second;
...
[StructLayout(LayoutKind.Explicit, Size=FOO_SIZE)]
public struct Foo  {
    [FieldOffset(0)] public ushort first;
    [FieldOffset(2)] public int second;
}

Any thoughts on how to achieve this?

[EDIT: See also my question on how to deal with the bytes when they are big endian.]

  • 1 1 Answer
  • 3 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-05-14T00:18:48+00:00Added an answer on May 14, 2026 at 12:18 am

    We have done this quite a bit as we talk directly to hardware via bytes over serial.

    Given the struct definition

    [StructLayout(LayoutKind.Explicit, Size=FOO_SIZE)]
    public struct Foo  {
        [FieldOffset(0)] public ushort first;
        [FieldOffset(2)] public int second;
    }
    

    You can use a class like this to perform the conversion

    public class ByteArrayToStruct<StructType>
    {
        public StructType ConvertToStruct(int size, byte[] thebuffer)
        {
            try
            {
                int theSize = size;
                IntPtr ptr1 = Marshal.AllocHGlobal(theSize);
                Marshal.Copy(thebuffer, 0, ptr1, theSize);
                StructType theStruct = (StructType)Marshal.PtrToStructure(ptr1, typeof(StructType));
                Marshal.FreeHGlobal(ptr1);
                return theStruct;
            }
            catch (Exception)
            {
                return default(StructType);
            }
        }
    }
    

    Conversely, you could also create List from the array and do something like the following:

    ushort first = BitConverter.ToInt16(myList.ToArray(), 0);
    myList.RemoveRange(0, sizeof(ushort));
    [...]
    

    This would essentially be keeping the relevant data at the “head” of the list, so you wont have to keep track of the position in the array.

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

Sidebar

Related Questions

Say I have a collection of @dogs, and I want to render part of
Say that I have the following code: using (var db = new MyDatabaseContext()) {
Say I have a collection of IList<Products> products And I want to group by
I have a collection of say 8 elements. I want to traverse it in
So say I have a collection of Bloops Class Bloop Public FirstName Public LastName
Let's say we have a collection of Person objects class Person { public string
Let's say I have a collection of objects which can be sorted using a
Another easy one hopefully. Let's say I have a collection like this: List<DateTime> allDates;
This is a long shot, I know... Let's say I have a collection List<MyClass>
Let's say I have a web service which returns a collection of images based

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.