Hex editing a file?

A

AlexUhde

Hello,

I am quite new to C# and so far I only used text files to read and
write from, but now I have a file which I normally open with a hex
editor and search for a value to change.
How can I do this in C#?

I am writing a program which should make it easier to change the
values in this file.
I was wondering if someone here could help me.

How can I open a file and search for a hex value (like 001A 0C35) and
than change this value and save back the file.
One problem is that the value can be on a different spot every time.

I hope that is enough info. I can explain more if needed.

I hope someone can help me!

Regards,
AlexUhde
 
J

Joe Cool

Hello,

I am quite new to C# and so far I only used text files to read and
write from, but now I have a file which I normally open with a hex
editor and search for a value to change.
How can I do this in C#?

I am writing a program which should make it easier to change the
values in this file.
I was wondering if someone here could help me.

How can I open a file and search for a hex value (like 001A 0C35) and
than change this value and save back the file.
One problem is that the value can be on a different spot every time.

I hope that is enough info. I can explain more if needed.

I hope someone can help me!


I think what you aew looking for is the BinaryReader class.
 
J

Joe Cool

Hello,

I am quite new to C# and so far I only used text files to read and
write from, but now I have a file which I normally open with a hex
editor and search for a value to change.
How can I do this in C#?

I am writing a program which should make it easier to change the
values in this file.
I was wondering if someone here could help me.

How can I open a file and search for a hex value (like 001A 0C35) and
than change this value and save back the file.
One problem is that the value can be on a different spot every time.

I hope that is enough info. I can explain more if needed.

I hope someone can help me!


I think what you aew looking for is the BinaryReader class.
 
A

AlexUhde

I think what you aew looking for is the BinaryReader class.

Hello,

So I cant do it in hex?
So I guess I need to convert the int values to binary and search for
them in the file?

I will search around for some BinaryReader tutorials. Any good you
know?

Regards,
Alexander Uhde
 
A

AlexUhde

I think what you aew looking for is the BinaryReader class.

Hello,

So I cant do it in hex?
So I guess I need to convert the int values to binary and search for
them in the file?

I will search around for some BinaryReader tutorials. Any good you
know?

Regards,
Alexander Uhde
 
A

AlexUhde

[...]
How can I open a file and search for a hex value (like 001A 0C35) and
than change this value and save back the file.
One problem is that the value can be on a different spot every time.
I hope that is enough info. I can explain more if needed.
I hope someone can help me!
I think what you aew looking for is the BinaryReader class.

So I cant do it in hex?

"Hex" is a way of representing a number as text.  Saying "hex" is only  
relevant if at some point, your number is to be represented as text, and  
there was nothing in your original post to suggest that was the case.
So I guess I need to convert the int values to binary and search for
them in the file?

Assuming the input is text, to do the search you will either have to  
convert from text to some integer type, or you will have to convert the  
data from the file from integer to text for each comparison.  Obviously 
the former is more efficient.  :)
I will search around for some BinaryReader tutorials. Any good you
know?

You should be more clear about what you really want to do.

Your original message implied that you already know the values you want to  
change and simply want to search for them in the file, replacing them with  
new values.  The BinaryReader or (IMHO more appropriate) FileStream  
classes both provide a byte-centric view of the file that can be used for 
that (with FileStream, you can use the same class to read and write...with  
BinaryReader, you'd have to also use BinaryWriter or FileStream to write  
the new values).

You can code integer literals in C# using hexadecimal, so if you know the 
values in advance but want to write them as hex, that's the way to do that.

If you want a more generalized program that allows the user to input the  
original and/or replacement values, then you'll want to look at the  
hexadecimal options for parsing text to integers (see "Custom Numeric  
Formats" and the int.Parse() method, for example).

With such a vague question, it's very difficult to provide specific  
advice.  If the above isn't useful enough, you really need to be more  
specific about what your exact question is.

Pete

Hello,
First let me say thanks to you both Peter and Joe!
you both helped me with my little project.

To explain a bit more what I want to do:

I have a file which is a saved game from Imperialism 2 (really old
game).
So instead of opening it in the Hex editor and as a little programming
project I decided to build a save game editor.

In short:
You write down how much paper and bronze you have in the game.
Load the save game in the app and than type it into two text boxes the
noted values.
The application searches for this two values in the save game and
displays the values as int in another set of text boxes.
In this second set of text boxes you can change the value to what ever
you want (from 1 to 9000).
Than hit save and the app writes the changed values to the save game
file.

I hope that this enough information.

Regards,
AlexUhde
 
A

AlexUhde

[...]
How can I open a file and search for a hex value (like 001A 0C35) and
than change this value and save back the file.
One problem is that the value can be on a different spot every time.
I hope that is enough info. I can explain more if needed.
I hope someone can help me!
I think what you aew looking for is the BinaryReader class.

So I cant do it in hex?

"Hex" is a way of representing a number as text.  Saying "hex" is only  
relevant if at some point, your number is to be represented as text, and  
there was nothing in your original post to suggest that was the case.
So I guess I need to convert the int values to binary and search for
them in the file?

Assuming the input is text, to do the search you will either have to  
convert from text to some integer type, or you will have to convert the  
data from the file from integer to text for each comparison.  Obviously 
the former is more efficient.  :)
I will search around for some BinaryReader tutorials. Any good you
know?

You should be more clear about what you really want to do.

Your original message implied that you already know the values you want to  
change and simply want to search for them in the file, replacing them with  
new values.  The BinaryReader or (IMHO more appropriate) FileStream  
classes both provide a byte-centric view of the file that can be used for 
that (with FileStream, you can use the same class to read and write...with  
BinaryReader, you'd have to also use BinaryWriter or FileStream to write  
the new values).

You can code integer literals in C# using hexadecimal, so if you know the 
values in advance but want to write them as hex, that's the way to do that.

If you want a more generalized program that allows the user to input the  
original and/or replacement values, then you'll want to look at the  
hexadecimal options for parsing text to integers (see "Custom Numeric  
Formats" and the int.Parse() method, for example).

With such a vague question, it's very difficult to provide specific  
advice.  If the above isn't useful enough, you really need to be more  
specific about what your exact question is.

Pete

Hello,
First let me say thanks to you both Peter and Joe!
you both helped me with my little project.

To explain a bit more what I want to do:

I have a file which is a saved game from Imperialism 2 (really old
game).
So instead of opening it in the Hex editor and as a little programming
project I decided to build a save game editor.

In short:
You write down how much paper and bronze you have in the game.
Load the save game in the app and than type it into two text boxes the
noted values.
The application searches for this two values in the save game and
displays the values as int in another set of text boxes.
In this second set of text boxes you can change the value to what ever
you want (from 1 to 9000).
Than hit save and the app writes the changed values to the save game
file.

I hope that this enough information.

Regards,
AlexUhde
 
A

AlexUhde

[...]
I have a file which is a saved game from Imperialism 2 (really old game).
So instead of opening it in the Hex editor and as a little programming
project I decided to build a save game editor.
In short:
You write down how much paper and bronze you have in the game.
Load the save game in the app and than type it into two text boxes the
noted values.
The application searches for this two values in the save game and
displays the values as int in another set of text boxes.
In this second set of text boxes you can change the value to what ever
you want (from 1 to 9000).
Than hit save and the app writes the changed values to the save game
file.
I hope that this enough information.

Probably it is.

My first comment: you run the risk of completely corrupting the data file 
with this approach.  There's no guarantee that the sequence of bytes that  
corresponds to the data you're trying to change doesn't also appear  
elsewhere in the file.  So, either you'll change the first appearance, in  
which case you may or may not change the data you want, or you'll change  
all appearances, in which case you'll change the data you want, but also  
possibly data you don't want to change.

If you want this to be reliable (and maybe in this case it's not that  
important :) ), you need to be doing more parsing of the file than just  
searching for specific numbers.  You really ought to incorporate logic in  
your program that knows more about the structure of the file and has a  
reliable way of finding exactly the position in the file where the data  
you want is.

The added benefit to that approach would be that you wouldn't have to tell  
the program the original values in the first place.  It would just display  
them to the user.  :)

If you insist on pursuing the original strategy, then most of what we've  
written so far still applies, except that you probably don't really need  
to be operating in hexadecimal.  As long as the numbers you want to change  
are stored as literally the actual numbers you see in the game, then you  
can do everything using decimal representation.  The general steps would  
be:

     -- Use int.Parse() on the text entered in the text boxes
     -- Use the BitConverter class to convert the int values to an array of  
bytes
     -- Use the FileStream class to read bytes from the file, comparing  
them to the bytes representing the data you're looking for
     -- When you find a match, use the FileStream class to write the new  
bytes you want (you'll have to use the Position property or Seek() method 
to set the file cursor back to the right place for writing)

Hope that helps.

Pete

I was thinking on the lines of searching for the values multiple times
always using the last place found + some bit as the start of the next
search and when the Search returns multiple results return an error.

Like I said in my first post. I am quite new to C# and I would have
(right now) no idea how to do more complex logic. but that might be a
part for Version 2.0.
because I would need to analyse the different save positions in the
save game. The game changes the spot where the values are stored by
every save.

Thank you for your Ideas and Help. I will have a look how I can do it
better!

Regards,
Alex
 
A

AlexUhde

[...]
I have a file which is a saved game from Imperialism 2 (really old game).
So instead of opening it in the Hex editor and as a little programming
project I decided to build a save game editor.
In short:
You write down how much paper and bronze you have in the game.
Load the save game in the app and than type it into two text boxes the
noted values.
The application searches for this two values in the save game and
displays the values as int in another set of text boxes.
In this second set of text boxes you can change the value to what ever
you want (from 1 to 9000).
Than hit save and the app writes the changed values to the save game
file.
I hope that this enough information.

Probably it is.

My first comment: you run the risk of completely corrupting the data file 
with this approach.  There's no guarantee that the sequence of bytes that  
corresponds to the data you're trying to change doesn't also appear  
elsewhere in the file.  So, either you'll change the first appearance, in  
which case you may or may not change the data you want, or you'll change  
all appearances, in which case you'll change the data you want, but also  
possibly data you don't want to change.

If you want this to be reliable (and maybe in this case it's not that  
important :) ), you need to be doing more parsing of the file than just  
searching for specific numbers.  You really ought to incorporate logic in  
your program that knows more about the structure of the file and has a  
reliable way of finding exactly the position in the file where the data  
you want is.

The added benefit to that approach would be that you wouldn't have to tell  
the program the original values in the first place.  It would just display  
them to the user.  :)

If you insist on pursuing the original strategy, then most of what we've  
written so far still applies, except that you probably don't really need  
to be operating in hexadecimal.  As long as the numbers you want to change  
are stored as literally the actual numbers you see in the game, then you  
can do everything using decimal representation.  The general steps would  
be:

     -- Use int.Parse() on the text entered in the text boxes
     -- Use the BitConverter class to convert the int values to an array of  
bytes
     -- Use the FileStream class to read bytes from the file, comparing  
them to the bytes representing the data you're looking for
     -- When you find a match, use the FileStream class to write the new  
bytes you want (you'll have to use the Position property or Seek() method 
to set the file cursor back to the right place for writing)

Hope that helps.

Pete

I was thinking on the lines of searching for the values multiple times
always using the last place found + some bit as the start of the next
search and when the Search returns multiple results return an error.

Like I said in my first post. I am quite new to C# and I would have
(right now) no idea how to do more complex logic. but that might be a
part for Version 2.0.
because I would need to analyse the different save positions in the
save game. The game changes the spot where the values are stored by
every save.

Thank you for your Ideas and Help. I will have a look how I can do it
better!

Regards,
Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top