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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:22:33+00:00 2026-06-03T06:22:33+00:00

I have two binary files, bigFile.bin and smallFile.bin . The bigFile.bin contains smallFile.bin .

  • 0

I have two binary files, "bigFile.bin" and "smallFile.bin".
The "bigFile.bin" contains "smallFile.bin".
Opening it in beyond compare confirms that.

I want to extract the smaller file form the bigger into a “result.bin” that equals "smallFile.bin".
I have two keywords- one for the start position (“Section”) and one for the end position (“Man”);

I tried the following:

   byte[] bigFile = File.ReadAllBytes("bigFile.bin");
   UTF8Encoding enc = new UTF8Encoding();
   string text =  enc.GetString(bigFile);

   int startIndex = text.IndexOf("Section");
   int endIndex = text.IndexOf("Man");

   string smallFile = text.Substring(startIndex, endIndex - startIndex);

   File.WriteAllBytes("result.bin",enc.GetBytes(smallFile));

I tried to compare the result file with the origin small file in beyond compare, which shows hex representation comparison.
nost of the bytes areequal -but some not.

For example in the new file I have 84 but in the old file I have EF BF BD sequence instead.

What can cause those differences? Where am I mistaken?

  • 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-06-03T06:22:34+00:00Added an answer on June 3, 2026 at 6:22 am

    Since you are working with binary files, you should not use text-related functionality (which includes encodings etc). Work with byte-related methods instead.

    Your current code could be converted to work by making it into something like this:

       byte[] bigFile = File.ReadAllBytes("bigFile.bin");
    
       int startIndex = /* assume we somehow know this */
       int endIndex = /* assume we somehow know this */
    
       var length = endIndex - startIndex;
       var smallFile = new byte[length];
       Array.Copy(bigFile, startIndex, smallFile, 0, length);
       File.WriteAllBytes("result.bin", smallFile);
    

    To find startIndex and endIndex you could even use your previous technique, but something like this would be more appropriate.

    However this would still be problematic because:

    1. Stuffing both binary data and “text” into the same file is going to complicate matters
    2. There is still a lot of unnecessary copying going on here; you should work with your input as a Stream rather than an array of bytes
    3. Even worse than the unnecessary copying, any non-stream solution would either need to load all of your input file in memory as happens above (wasteful), or be exceedingly complicated to code

    So, what to do?

    1. Don’t read file contents in memory as byte arrays. Work with FileStream instead.
    2. Wrap a StreamReader around the FileStream and use it to find the markers for the start and end indexes. Even better, change your file format so that you don’t need to search for text.
    3. After you know startIndex and length, use stream functions to seek to the relevant part of your input stream and copy length bytes to the output stream.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two binary files with different sizes. I need to compare first N
I have two binary files that are related one to another (meaning, when one
I have two binary files (order of tens of MB) and I want to
I have two binary files equal in size, but not in values. I used
I have two files (f1 and f2) containing some text (or binary data). How
I have two binary input files, firstfile and secondfile . secondfile is firstfile +
I have a program that takes two files as an argument. The first file
Is there a Linux command-line app which will compare two binary files and exit
Have two folders with approx. 150 java property files. In a shell script, how
I have two application that need to talk to each other. App1 needs to

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.