• 0

[VB.Net][C#] Replacing some bytes with another bytes in binary file


Question

  • 0

Simple one byte example:

//
// open file.bin (in local directory) for read/write access
//

BinaryWriter bw = new BinaryWriter(
	File.Open(@"file.binFiFileMode.Open, FileAccess.ReadWrite));

//
// write byte (character 'b') at offset 1234
// and close the file
//

bw.BaseStream.Seek(0x1234, SeekOrigin.Begin);
bw.Write(0x62);
bw.Close();

Simple multi-byte example:

//
// not a real patch, this is JMP EIP;p
//

byte[] patch = { 0xEB, 0xFE };

//
// open file.bin (in local directory) for read/write access
//

BinaryWriter bw = new BinaryWriter(
	File.Open(@"file.binFiFileMode.Open, FileAccess.ReadWrite));

//
// write byte (character 'b') at offset 1234
// and close the file
//

bw.BaseStream.Seek(0x1234, SeekOrigin.Begin);
bw.Write(patch);
bw.Close();

7 answers to this question

Recommended Posts

  • 0

ok never mind, i managed to replace the byte with your code, thanks again :)

However it seems that i MUST replace bytes with same length. I can't replace 2 bytes with 7 bytes for example.

Edited by Elagizy
  • 0
  Elagizy said:
ok never mind, i managed to replace the byte with your code, thanks again :)

However it seems that i MUST replace bytes with same length. I can't replace 2 bytes with 7 bytes for example.

No, BinaryWriter cannot "insert" bytes. Most binary files, like executables, cannot change in size without having to alter a bunch of other information (i.e. PE header). Your best bet is to explain what you're trying to do; we'll try to determine what the best solution is.

  • 0

I managed to record LIVE MMS streams, unfortunately after saving streams, it only works on VLC player. One of the issues i'm working on, is to fix ASF header "play_duration" to get the exact duration of the file. But the file will still be playable on VLC player only, but I can now see the file play length and can drag the seek bar.

Some LIVE MMS streams plays well on all media players, so i'm trying to compare asf headers with each other and fill up the gap.

It seems to be working, i use UltraEdit to mess with ASF header and i got some files partially fixed. All i need at the moment is to edit bytes freely programmatically.

But your code also helps me alot, ASF header has some values with fixed length like "packet length" "play duration" ...etc

  • 0
  Elagizy said:
I managed to record LIVE MMS streams, unfortunately after saving streams, it only works on VLC player. One of the issues i'm working on, is to fix ASF header "play_duration" to get the exact duration of the file. But the file will still be playable on VLC player only, but I can now see the file play length and can drag the seek bar.

Some LIVE MMS streams plays well on all media players, so i'm trying to compare asf headers with each other and fill up the gap.

It seems to be working, i use UltraEdit to mess with ASF header and i got some files partially fixed. All i need at the moment is to edit bytes freely programmatically.

But your code also helps me alot, ASF header has some values with fixed length like "packet length" "play duration" ...etc

You should use a bit stream library which will allow you to write and read stuff at a bit level.

http://www.codeproject.com/csharp/bitstream.asp

This should be pretty straight forward.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Popular Now

  • Posts

    • There is a lot of truth to that, but also, a lot of those issues were due to 3rd parties being slow to support Vista. That doesn't explain all users, but a lot of them. Win7 didn't have better support for XP-era hardware and software, it simply didn't have to deal with it as much because Vista has already blazed the way and forced that stuff to be updated or replaced.
    • MIght as well go back to good old SMS. Or switch to other services.
    • Nintendo 64 games were $60-$70 Playstation 1 were $50-$60 28 years ago. Heck I remember paying $70 for Final Fantasy IV (2) in 1991 for SNES. Salarys have gone up, both for the consumer's salary and the developer's salary. So I think it's pretty reasonable for $80games today. Not that I pay for new games, I don't have the time to play games as much as before. So I tend to buy them 2 years after release.
    • I hear you on that, and if that is happening, then its bad. Could you give an example please? People make these comments all the time but rarely provide an example.
    • Firefox gets new way to pin and unpin tabs by Taras Buria Mozilla Firefox recently received a long-anticipated feature: vertical tabs. It arrived in March 2025 as part of the Firefox 136 update. Now, Mozilla is introducing another welcome improvement to tab management in its browser, making it easier for users to pin and unpin tabs in Firefox. With the latest Firefox Nightly update, Mozilla implemented a new way to pin or unpin tabs: you can simply drag the current page to another pinned tab to pin or drag it out to unpin it. As simple as that. The new logic works with both vertical and horizontal tab views, and it eliminates the need to use context menus. The only prerequisite is that you need to have at least one pinned tab for the dragging to work. Otherwise, you will simply move the tab across the strip. It is nice to see Firefox implementing this small convenience for customers. However, some argue that there is still room for improvement. Dragging tabs is cool, but a dedicated shortcut could be an even better and quicker way to pin or unpin your tabs. That said, other mainstream browsers lack this feature as well. The improved tab pinning is now available in Firefox Nightly, the least stable update channel. Mozilla uses it to test early changes and big platform changes, which could be buggy, unstable, or feature-breaking. Therefore, it is probably not a very good idea to use Nightly builds as your main browser. Still, you can run Firefox Nightly side-by-side with other channels for testing purposes. If you are curious, get it from the official website using this link.
  • Recent Achievements

    • Week One Done
      patrickft456 earned a badge
      Week One Done
    • One Month Later
      patrickft456 earned a badge
      One Month Later
    • One Month Later
      Jdoe25 earned a badge
      One Month Later
    • Explorer
      Legend20 went up a rank
      Explorer
    • One Month Later
      jezzzy earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      636
    2. 2
      ATLien_0
      278
    3. 3
      +FloatingFatMan
      171
    4. 4
      Michael Scrip
      156
    5. 5
      Steven P.
      128
  • Tell a friend

    Love Neowin? Tell a friend!