473,411 Members | 2,186 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

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 8983
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.com>
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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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.com>
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.com>
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
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...
8
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...
3
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
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...
6
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...
11
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...
1
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....
3
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...
27
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? ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...

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.