473,783 Members | 2,564 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to replace some bytes of a binary file

I have a file, and want to replace some bytes by other bytes e.g.

Old: 1b 25
New: 1b 26 66 31 30 30 59 1b 26 66 58

How to implement? Thanks!
Nov 15 '05 #1
3 9042
Mullin Yu <mu*******@ctil .com> wrote:
I have a file, and want to replace some bytes by other bytes e.g.

Old: 1b 25
New: 1b 26 66 31 30 30 59 1b 26 66 58

How to implement? Thanks!


You'll need to open a new file, and read in the old file, writing
unchanged bytes to the new file as they are and changing the others.
Then rename the old file to a backup, delete the old file, rename the
new file to the old file, then remove the backup. (If you don't care
too much if something goes wrong, you could just delete the old file
and then rename the new file without bothering with the backup.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
but, if i read the file byte by byte, how can i know that it's 1b 25??? if i
want to have a generic solution, how to handle then?

do i need to convert all the bytes from files to string with delimiter like
1b;25;
1b;26;66;31;30; 30;59;1b;26;66; 58

then, i can use the string replace function and then convert them to array
and write back to stream.

but, any issue if the file size is very large.

thanks!

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Mullin Yu <mu*******@ctil .com> wrote:
I have a file, and want to replace some bytes by other bytes e.g.

Old: 1b 25
New: 1b 26 66 31 30 30 59 1b 26 66 58

How to implement? Thanks!


You'll need to open a new file, and read in the old file, writing
unchanged bytes to the new file as they are and changing the others.
Then rename the old file to a backup, delete the old file, rename the
new file to the old file, then remove the backup. (If you don't care
too much if something goes wrong, you could just delete the old file
and then rename the new file without bothering with the backup.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #3
Mullin Yu <mu*******@ctil .com> wrote:
but, if i read the file byte by byte, how can i know that it's 1b 25???
You'll need some kind of table saying, "If the first byte is 1b, then
don't write it yet, but remember that I've received it; if the next
byte is 25 then write the replaced version, otherwise write 1b and then
the received byte."
if i want to have a generic solution, how to handle then?
As above, but in a generic way. Basically, for any byte received, you
either know that it (and possibly some "stored" bytes) should be
written as they are, or that you've come to the end of a path and
should write out a different load of bytes instead.
do i need to convert all the bytes from files to string with delimiter like
1b;25;
1b;26;66;31;30; 30;59;1b;26;66; 58

then, i can use the string replace function and then convert them to array
and write back to stream.
Converting to a string and back would be a bad idea, IMO. Text and
binary should not be confused. Unless, of course, it's actually a text
file to start with...
but, any issue if the file size is very large.


Yes - loading the whole thing into memory at once is a bad idea if it's
large.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3292
by: Jon Slaughter | last post by:
I'm trying to replace some bytes in a file using fstream but all I seem to be able to do is append or 0 the file then write... Basicaly I just need to replace the first 512 bytes of the file with some buffer(but eventually I will need to replace random blocks in the file at different locations). Is there anyway to do this efficiently using fstream? (I know I can duplicate the file, but it seems a waste to rewrite the same data for no...
8
4068
by: Eric Lilja | last post by:
Hello, I had what I thought was normal text-file and I needed to locate a string matching a certain pattern in that file and, if found, replace that string. I thought this would be simple but I had problems getting my algorithm to work and in order to help me find the solution I decided to print each line to screen as I read them. Then, to my surprise, I noticed that there was a space between every character as I outputted the lines to the...
3
2428
by: RickN | last post by:
What is the best way to open a file and perform a search and replace for multiple char values. Thanks, RickN
5
3662
by: philip | last post by:
Here is some lines of code than I wrote. You can copy/paste theis code as code of form1 in a new project. My problem is this one : I try to write in a file a serie of bytes. BUT some bytes written in file are not the sent bytes. Copy and paste the following lines to observe my problem. What can I do to resolve problem ? Only System.Text.Encoding.ASCII write the same number of bytes, but not the good bytes. Someone can help me. Thanks by...
6
5811
by: =?Utf-8?B?VGhvbWFzWg==?= | last post by:
Hi, Is it possible to read a file in reverse and only get the last 100 bytes in the file without reading the whole file from the begining? I have to get info from files that are in the last 100 bytes of the file and some of these files are 600Mb -1 GB in size. I am getting "outofMemory.." exceptions on the largest files and the other files take "forever" to get the last 100 bytes. This is the code I have currently that works with...
11
3595
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function Read_bin(ByVal ruta As String) Dim cadena As String = "" Dim dato As Array If File.Exists(ruta) = True Then
1
3145
by: | last post by:
dim br as BinaryReader(stream) dim buf(5000) as byte br.Read(buf,0,5000) closeit.. I have read a binary file into a buffer. This buffer contains many different chars as well as a few null chars. I want to replace the (NULLS)chr(0)'s with (SPACES)chr(32)'s. How can I do this. REPLACE does not seem to be an option, If I converted it to a string I was afraid the string would stop at the first NULL truncating the remaining chars. Any ideas...
3
14572
by: mouac01 | last post by:
Newbie here. How do I do a find and replace in a binary file? I need to read in a binary file then replace a string "ABC" with another string "XYZ" then write to a new file. Find string is the same length as Replace string. Here's what I have so far. I spent many hours googling for sample code but couldn't find much. Thanks... public static void FindReplace(string OldFile, string NewFile) { string sFind = "ABC"; //I probably need...
27
7548
by: Jason | last post by:
Hi, I need to open an existing file, seek to a position at X number of bytes, and write out Y number of bytes overwriting any existing bytes, but no erasing any other data. Is this possible? I've opened a file in append "a" mode, and then used fseek with SEEK_SET to seek to 0 bytes into the file, but this sets the position at the end of existing data, not actually at the start of the file.
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10083
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.