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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:50:55+00:00 2026-05-31T21:50:55+00:00

I have a binary file that I would like to regex search/replace hex bytes

  • 0

I have a binary file that I would like to regex search/replace hex bytes within it. I know there are command line methods for achieving this with Perl, however, if there’s a way to do this with Objective-C/Cocoa I haven’t been able to find it. The Perl method from the command line in OSX which works fine, although I would like to incorporate such in a Cocoa application – thanks

Binary file: TEST

45 76 65 6E 20 69 66 20 79 6F 75 20 66 61 6C 6C 20 6F 6E 20 79 6F 75 72 20 66 61 63 65 2C 20 79 6F 75 27 72 65 20 73 74 69 6C 6C 20 6D 6F 76 69 6E 67 20 66 6F 72 77 61 72 64 2E 20 

Bytes to replace: 66 61 63
Replace with: 61 72 73

Perl example:

# grep -Z -r -l face TEST | xargs -0 perl -pi -e "s/\x66\x61\x63/\x61\x72\x73/g"
  • 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-31T21:50:56+00:00Added an answer on May 31, 2026 at 9:50 pm

    Something along the lines of this should work:

    // Change TEST to the absolute path of the file to read the data from
    NSString *fileName = @"TEST";
    NSMutableData *data = [NSMutableData dataWithContentsOfFile:fileName];
    if (data == nil || [data length] == 0) {
        return;
    }
    

    Your original question:

    char bytes[]     = {0x66, 0x61, 0x63};
    char new_bytes[] = {0x61, 0x72, 0x73};
    
    NSRange range = [data rangeOfData:[NSData dataWithBytes:bytes length:3] options:0 range:NSMakeRange(0, [data length])];
    if (range.location == NSNotFound) {
        return;
    }
    
    [data replaceBytesInRange:range withBytes:new_bytes];
    [data writeToFile:fileName atomically:YES];
    

    Edited for wildcard (I just used -1 for the wildcard and it can be in any spot):

    char bytes[]     = {0x66, -0x1, 0x63};
    char new_bytes[] = {0x61, 0x72, 0x73};
    
    NSUInteger dataLength = [data length];
    if (dataLength < 3) {
        return;
    }
    
    char *data_bytes = (char *)[data bytes];
    
    int dataLengthMinus3 = dataLength - 3;
    
    for (NSUInteger i = 0; i < dataLengthMinus3; i++) {
        if (bytes[0] == -0x1 || data_bytes[i] == bytes[0]) {
            if (bytes[1] == -0x1 || data_bytes[i+1] == bytes[1]) {
                if (bytes[2] == -0x1 || data_bytes[i+2] == bytes[2]) {
                    for (int z = 0; z < 3; z++) {
                        if (new_bytes[z] != -0x1){
                            [data replaceBytesInRange:NSMakeRange(i+z, 1) withBytes:new_bytes[z]];
                        }
                    }
                    i += 2;
                    continue; // Make this break if you only want to replace the first occurrence.
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ~700 MB binary file (non-text data); what I would like to
I have a file that has both ASCII text and binary content. I would
We have large files with zlib-compressed binary data that we would like to memory
Would like to ask for advice. there is a need for binary to have
I have a binary file that I have to parse and I'm using Python.
I have binary data in a file that I can read into a byte
I have response stream from a ftp web request that returns binary file. I
I have a binary file - Windows static library (*.lib). Is there a simple
I have one binary file and I want to read this file like first
I have a program that downloads a binary file, from another PC. I also

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.