I am working on a project where I read data from a file and I need to manipulate some of this data.
The data is binary with some ASCII encoded text in it.
The data is also saved Big Endian if that matters.
What I would like to accomplish is to find a pattern with in this data and manipulate parts of that pattern.
Example: (09 49 6E 76 65 6E 74 6F 72 79 0A 00 00 00 02 01 00)
This represents the number 9 for how many letters are in Inventory, followed by ASCII "Inventory" without quotes. "0A" Marks the end of that ASCII text, followed with 00 00 00 02 to mark the size of our inventory "2". "01 00" Marks the end of The entire inventory region.
Example2: (04 53 6C 6F 74 00 02 00)
This represents the number 4 for how many letters are in Slot, followed by ASCII "Slot" without quotes. "00" is a space between ASCII text and Slot number "02", followed by end of region "00".
I need to locate these patterns and several others within the file.
I then need to modify parts of the pattern and write to disk.
ExampleModify: (04 53 6C 6F 74 00 02 00) "From above" to (04 53 6C 6F 74 00 07 00) changing "slot number ’02’ to ’07’.
Another caveat is that while I have several patterns that I need to search in the file of varying size, length, and data contained, is that there can be multiple sections of these patterns containing different data that needs to be modified separately as a whole.
To clarify: (Inventory, Slot, id, Count) – is to be treated as information for one person.
There can be multiple copies of (Inventory, Slot, id, Count) for each person that is recorded.
I would like to display this information to the user, and give them the option to modify each of the elements in the group.
I am not a good programmer and want to learn, if you have examples I appreciate that, if you have advice please give it. If you can dumb it down that is even better, thanks.
I have a work-in-progress going now, but I am stuck now. If you want to see what I have please let me know.
Summary of what I have: Read file into byte[] then display the whole array to console. That’s about it. with a little bit of formatting and some debug info for locating that chunk I read into the array.
Here is a link to my code on pastebin. LINK
I realize that I do not get all occurrences of (Inventory, Slot, id, Count) I need to fix that as well.
EDIT: Example (09 49 6E 76 65 6E 74 6F 72 79 0A 00 00 00 02 01 00) this is a fixed length chunk of binary data within the file I’m reading. As stated 09 represents the length of the string. what follows the string is (0A 00 00 00 02 01 00) the important part of that is (02)
this is because that is the only byte(s) that change in that slice of binary data. "02" represents "2" meaning there are 2 instances of (Slot, id, Count) for that particular persons record.
(File)
(09 Inventory 0A 00 00 00 02 01 00) // Start of person 1's record with 2 instances.
(Slot)
(id)
(Count)
(Slot)
(id)
(Count)
(Rotation) // End of person 1's record
(09 Inventory 0A 00 00 00 04 01 00) // Start of person 2's record with 4 instances.
(Slot)
(id)
(Count)
(Slot)
(id)
(Count)
(Slot)
(id)
(Count)
(Slot)
(id)
(Count)
(Rotation) // End of person 2's record
(File End)
The idea is that I want to edit the "id" or increase the number of slots by adding to the inventory and add in more instances of (Slot, id, Count).
"id" – contains item id
"Slot" – contains inventory slot number
"Count" – contains how many of item is in that slot.
EDIT Note: Please let me know if I am not being clear, thank you again.
You said you’re not a good programmer. I’ll make some basic suggestions then.
First, for any problem, break it down. Try to break it up into simpler sub-problems that you can solve, or think will be easier to solve. You said you wanted to check for patterns in a file, and then maybe modify some of the data and write it back out. As a first step, I’d say these are the major sub-problems you need to solve in order to get to a complete solution.
You focused mostly on “search for patterns” part, so I will focus further comments on that. All those other pieces can also be tricky, but you can either solve them yourself or look for hints on stackoverflow or in other places for how other people solved them.
ok, now, regarding “search for patterns” . In my reading of your description,
It seems to me the first byte in your byte array is the text length. In C# code, a data array that exhibits your pattern could be declared explicitly like this:
of course, you won’t be declaring the array explicitly. You’ll be creating or filling array via the file-read operation. The file-read is still an open problem at this point, but that’s ok – you can solve it separately. And, the effect is the same – when you have successfully read the file, you will have a byte array that looks like the array that is explicitly declared above.
ok, now how do you manipulate that data? The first byte is the length of string data. The next N bytes are the string data. Then you have some other stuff.
you didn’t describe exactly what “modifications” you want to make, but with the information you have provided, it should be easy to walk through the data.
If you want to get a “slice” of the data in an array, you can do this .
The next thing you might want to do is get an actual string out of those bytes you’ve sliced out. To do that, use the Encoding class:
I don’t understand the pattern for the data that follows the string data. In some cases it’s a
00, in some cases it’s0A.But maybe you know the pattern. Follow along the same way and you can walk through all the data for one “record”. When you get to the end of the record, then start again and process the next one. The key to getting to the next “Record” is knowing how large a slice of data to take out of the data you have read from the file.
This brings us back to the “reading the file” sub-problem. One way to get the right amount of data from the file is to read only the first byte, then read N bytes further in the file. (See System.IO.Stream.Read).
At this point you can read the next N bytes:
Now you can get the string.
…and so on. Read all the trailing bytes. At that point the cursor that is implicitly held by the
fileStreampoints to the next record in the file.