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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:37:08+00:00 2026-05-23T14:37:08+00:00

If I wanted to fill a structure from a binary file, I would use

  • 0

If I wanted to fill a structure from a binary file, I would use something like this:

using (BinaryReader br = new BinaryReader(File.Open(filename, FileMode.Open)))
{
    myStruct.ID = br.ReadSingle();
    myStruct.name = br.ReadBytes(20);
}

However, I must read the whole file into a byte array before deserializing, because I want to do some pre-processing. Is there any managed way to fill my structure from the byte array, preferably similar to the one above?

  • 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-23T14:37:09+00:00Added an answer on May 23, 2026 at 2:37 pm

    This is a sample to take some data (actually a System.Data.DataSet) and serialize to an array of bytes, while compressing using DeflateStream.

    try
    {
        var formatter = new BinaryFormatter();
        byte[] content;
        using (var ms = new MemoryStream())
        {
             using (var ds = new DeflateStream(ms, CompressionMode.Compress, true))
             {
                 formatter.Serialize(ds, set);
             }
             ms.Position = 0;
             content = ms.GetBuffer();
             contentAsString = BytesToString(content);
         }
    }
    catch (Exception ex) { /* handle exception omitted */ }
    

    Here is the code in reverse to deserialize:

            var set = new DataSet();
            try
            {
                var content = StringToBytes(s);
                var formatter = new BinaryFormatter();
                using (var ms = new MemoryStream(content))
                {
                    using (var ds = new DeflateStream(ms, CompressionMode.Decompress, true))
                    {
                        set = (DataSet)formatter.Deserialize(ds);                        
                    }
                }
            }
            catch (Exception ex)
            {
                // removed error handling logic!
            }
    

    Hope this helps. As Nate implied, we are using MemoryStream here.

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

Sidebar

Related Questions

I wanted to do something like this: <asp:Label ID=lblMyLabel onclick=lblMyLabel_Click runat=server>My Label</asp:Label> I know
I'm reading binary data from a file, specifically from a zip file. (To know
If I wanted to fill my game screen with individually coloured pixels, how would
I'm using the jcarousel library (http://sorgalla.com/projects/jcarousel/) and wanted to use the vertical carousel, but
I wanted to generate one fix view using interface builder, but the size of
I wanted to open up the topic to discuss ways to encourage or incentivize
I just installed a fresh new version of Ubuntu 10.04 and wanted to get
I need to develop a file indexing application in python and wanted to know
I wanted to create a string padding function for the use of left-padding a
I wanted to create a layout like the CyanogenMod call log in that there

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.