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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:30:44+00:00 2026-05-26T23:30:44+00:00

This is more for practice than anything, really. I am having the most frustrating

  • 0

This is more for practice than anything, really. I am having the most frustrating time with this as it’s a fairly new concept to me. I will post my code below.

What I am attempting to do:

  1. Read a file into a byte array
  2. Split the byte into pars of a predefined size
  3. Put the parts back together, and write the file to HD

    byte[] sData = File.ReadAllBytes(@”C:\Project1.exe”); // 16,384 bytes

    // Split the data up here
                    int range = 8;
                    range *= 1024;
                    int pos = 0;
                    int remaining;
                    int i = 0;
                    byte[] test = null;
                    while ((remaining = sData.Length - pos) > 0)
                    {
                        byte[] block = new byte[Math.Min(remaining, range)];
                        test = new byte[block.Length + pos];
                        Array.Copy(sData, pos, test, pos, block.Length);
                        pos += block.Length;
                        i++;
                    }
    
                    File.WriteAllBytes(@"C:\blank.exe", test);
    

The file “blank.exe” is always corrupt.

Does anyone see my error(s) here?

I appreciate it, Evan

  • 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-26T23:30:44+00:00Added an answer on May 26, 2026 at 11:30 pm

    You are recreating the test array on each pass through the loop.

    This means when you write the test array to the file at the end, you are only writing the last block of data that you processed.

    You have a few options:

    1) Resize the array on each pass and copy the previous data into the new array. This would be very inefficient. This is the same mechanism that Array.Resize uses.

    2) If you know the desired size of the array ahead of time (i.e. it is the same size as the data you read from the file or a multiple of the file size), then just resize the array one time before entering the loop.

    3) Use a different data structure, such as as List or an ArrayList.

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

Sidebar

Related Questions

This is more a design question than anything else... I really like Scala's case
This is more of a conceptual question than anything. I have a base class
This is more of an architecture/best practices question than anything else, so please feel
This is more a best practice question than a language question in itself, since
This is more of a question about best pattern and practice than asking about
This is more of an academic inquiry than a practical question. Are there any
This is more an observation than a real question: MS-Access (and VBA in general)
Note: This is more of a best how rather than how question. I have
This is more of a 'How should I?' rather than 'How do I?' question.
This is more an MBCS question than a Unicode question. I need to create

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.