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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:35:02+00:00 2026-06-13T10:35:02+00:00

I have a binary file (i.e., it contains bytes with values between 0x00 and

  • 0

I have a binary file (i.e., it contains bytes with values between 0x00 and 0xFF). There are also ASCII strings in the file (e.g., “Hello World”) that I want to find and edit using Regex. I then need to write out the edited file so that it’s exactly the same as the old one but with my ASCII edits having been performed. How?

        byte[] inbytes = File.ReadAllBytes(wfile);
        string instring = utf8.GetString(inbytes);
        // use Regex to find/replace some text within instring
        byte[] outbytes = utf8.GetBytes(instring);
        File.WriteAllBytes(outfile, outbytes);

Even if I don’t do any edits, the output file is different from the input file. What’s going on, and how can I do what I want?


EDIT: Ok, I’m trying to use the offered suggestion and am having trouble understanding how to actually implement it. Here’s my sample code:

        string infile = @"C:\temp\in.dat";
        string outfile = @"C:\temp\out.dat";
        Regex re = new Regex(@"H[a-z]+ W[a-z]+");  // looking for "Hello World"
        byte[] inbytes = File.ReadAllBytes(infile);
        string instring = new SoapHexBinary(inbytes).ToString();
        Match match = re.Match(instring);
        if (match.Success)
        {
            // do work on 'instring'
        }
        File.WriteAllBytes(outfile, SoapHexBinary.Parse(instring).Value);

Obviously, I know I’ll not get a match doing it that way, but if I convert my Regex to a string (or whatever), then I can’t use Match, etc. Any ideas? Thanks!

  • 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-13T10:35:04+00:00Added an answer on June 13, 2026 at 10:35 am

    I got it! Check out the code:

            string infile = @"C:\temp\in.dat";
            string outfile = @"C:\temp\out.dat";
            Regex re = new Regex(@"H[a-z]+ W[a-z]+");   // looking for "Hello World"
            string repl =  @"Hi there";
    
            Encoding ascii = Encoding.ASCII;
            byte[] inbytes = File.ReadAllBytes(infile);
            string instr = ascii.GetString(inbytes);
            Match match = re.Match(instr);
            int beg = 0;
            bool replaced = false;
            List<byte> newbytes = new List<byte>();
            while (match.Success)
            {
                replaced = true;
                for (int i = beg; i < match.Index; i++)
                    newbytes.Add(inbytes[i]);
                foreach (char c in repl)
                    newbytes.Add(Convert.ToByte(c));
                Match nmatch = match.NextMatch();
                int end = (nmatch.Success) ? nmatch.Index : inbytes.Length;
                for (int i = match.Index + match.Length; i < end; i++)
                    newbytes.Add(inbytes[i]);
                beg = end;
                match = nmatch;
            }
            if (replaced)
            {
                var newarr = newbytes.ToArray();
                File.WriteAllBytes(outfile, newarr);
            }
            else
            {
                File.WriteAllBytes(outfile, inbytes);
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say we have a binary file that contains 2 bytes that form an
I have a binary file that contains blocks of information ( I'll refer to
I have a binary file, that was written in C, which contains a long
I have a binary file that I would like to regex search/replace hex bytes
I have a binary file that contains an X by X matrix. The file
I have a binary file. This file contains an UTF-8 string. Moreover, it is
I have a binary file I've loaded using an NSData object. Is there a
we have a text file containing binary values. say, we have a file file.txt,it
i have a binary file with following format : [N bytes identifier & record
I have a large binary file that contains all the information I want 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.