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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:58:05+00:00 2026-05-25T00:58:05+00:00

in this case a binary file is written with the file format based on

  • 0

in this case a binary file is written with the file format based on a struct

    struct fileformat
    {
         struct mask
         {
                bool mem1present
                bool mem2present
                bool mem3present
                //5 bits unused
          }
          //member only written in file if mem1present is true
          byte mem1present
          //member only written in file if mem2present is true
          byte mem1present
          //member only written in file if mem3present is true
          byte mem1present
    }

is this possible to be implemented in 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. Editorial Team
    Editorial Team
    2026-05-25T00:58:06+00:00Added an answer on May 25, 2026 at 12:58 am

    Sure – you have to implement the serialization yourself to some extent, but you can do that easily enough.

    It’s unclear what sort of serialization you’re using – if you’re using the “raw” binary serialization from .NET, you want to override GetObjectData to only add the relevant data on serialization, and then in the protected constructor taking a SerializationInfo and a StreamingContext, populate your struct from the same data in reverse. See this MSDN article for some details.

    I don’t know what happens if you’re using XML serialization.

    If you’re writing your own serialization (i.e. you’ve got a method such as WriteToStream) then you can choose to represent it however you want, of course.

    EDIT: It sounds like you’ve probably got an existing file format you need to read in, but you can define your own types. It’s easy to have a class or struct with multiple members and possibly a mask to say what’s set, although without knowing more it may not be the best design. While you can use explicit layout to make this efficient in memory, it’s probably easiest just to have separate members:

    struct Foo
    {
        // Bit-set to determine which fields are actually used
        private readonly byte mask;
    
        private readonly int value1;
        private readonly int value2;
        private readonly int value3;
    
        public Foo(byte mask, int value1, int value2, int value3)
        {
            this.mask = mask;
            this.value1 = value1;
            this.value2 = value2;
            this.value3 = value3;
        }
    }
    

    Then somewhere (either in the data type or not), something like:

    Foo ReadFoo(Stream stream)
    {
        byte mask = stream.ReadByte();
        int value1 = 0, value2 = 0, value3 = 0;
        if ((mask & 1) == 1)
        {
           // However you do that, depending on your file format
            value1 = ReadInt32FromStream(stream); 
        }
        if ((mask & 2) == 2)
        {
           // However you do that, depending on your file format
            value2 = ReadInt32FromStream(stream); 
        }
        if ((mask & 4) == 4)
        {
           // However you do that, depending on your file format
            value3 = ReadInt32FromStream(stream); 
        }
        return new Foo(mask, value1, value2, value3);
    }
    

    By the way, I would seriously consider whether a struct is really the best approach here – consider using a class instead. I very rarely create my own structs.

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

Sidebar

Related Questions

I'm reading in a binary file (a jpg in this case), and need to
I'm trying to submit a binary file, in this case, an Excel file from
I have a binary file and documentation of the format the information is stored
I have a grails webservice that takes a binary file as a parameter. This
I am developing a .NET (C#) application with a binary file format. The plan
So I have some Binary data in python (a jpg Image, in this case)
Why should I use a human readable file format in preference to a binary
I have moderately large binary file consisting of independent blocks like this: header1 data1
Consider this case: dll = LoadDLL() dll->do() ... void do() { char *a =
Imagine this case where I have an object that I need to check a

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.