Read bytes from binary file

rainbow-girl

Member
Joined
Jul 27, 2024
Messages
12
Programming Experience
Beginner
Hi

I'm opening a binary file in a HEX editor. In this file I want so save the first 8 bytes to another destination file.
I want to read this binary file by using C# code and save the first 8 bytes of the file to a destination binary file (that will contain only those 8 bytes), all by using C# code.

Can somebody please give me a code example that can do this?

Thanks so much!!!
Lisa
 
Unfortunately, this site isn't a gimme-the-codez site, nor a pay-someone-to-do-my-homework site. The way this site works is you show your code and tell us what issues you have encountered, as well as, what you've tried to fix the issue.

Big hint: You can open files as streams, and these let you read and write byte arrays.
 
A lot of people say "example" when what they actually mean is "the precise code to do exactly what I want". There are undoubtedly examples of reading bytes from files and writing bytes to files already on the web. You can find them by searching for expressions like "c# read bytes from file". You should be using those existing examples to learn the principles and then making your best attempt to implement those principles to do what you want to do. If what you try doesn't work, then you have a question for us. You can explain exactly what you're trying to achieve, show us how you're trying to achieve it, explain what happens when you try and exactly where and how that differs from your expectations. That's how this works: you try, you fail, we help you fix it, you succeed. Failure is an integral part of learning.
 
A lot of people say "example" when what they actually mean is "the precise code to do exactly what I want". There are undoubtedly examples of reading bytes from files and writing bytes to files already on the web. You can find them by searching for expressions like "c# read bytes from file". You should be using those existing examples to learn the principles and then making your best attempt to implement those principles to do what you want to do. If what you try doesn't work, then you have a question for us. You can explain exactly what you're trying to achieve, show us how you're trying to achieve it, explain what happens when you try and exactly where and how that differs from your expectations. That's how this works: you try, you fail, we help you fix it, you succeed. Failure is an integral part of learning.

Is this the right example I need maybe? So I need a BinaryReader ? Then i'll try it :)

 
The example there is for the situation when you have more complex data types which you need to easily serialize and deserialize into a binary stream. You just need to read and write an array of bytes directly. Read the documentation of the class that they presented above the code example. Notice that there are methods which can handle byte arrays.

Also notice that a FileStream is passed into the BinaryReaderor BinaryWriter. If you look at the documentation for FileStream you will see that it also has methods for reading and writing byte arrays. That suggests that you do not even need the binary reader and writer classes.
 
As suggested, the BinaryWriter class is for writing objects to binary files as bytes and the BinaryReader is reading those bytes back into objects. For reading and writing raw bytes, the FileStream is all you need. Note that, when dealing with files, any other types used for reading and writing data in particular ways, e.g. StreamReader and StreamWriter for text, will use a FileStream under the hood.
 
In another project, I also need to be able to print the values of the bytes as in a HEX editor (at the right side). See screenshot for some selected bytes and at the right the values. How do I do that if I only use a FileStream?

1722185128651.png
 
UI is independent of file I/O. Once you have the bytes in memory, you can display them however you want. So it doesn't matter if you use a FileStream or something else.
 
Does it mean I have to typecast a byte to whatever I want or is this not the way to do it? For example my screenshot each byte will be typecast to (char) ?
 
Be aware that in the .NET Framework, and therefore in C#, strings use UTF-16 and so each character is 2 bytes each. C# is not C or C++. You cannot just type cast a pointer to an array of bytes into a pointer to a string, specially if the bytes are ASCII, UTF7, or UTF8 encoded.

If you know that the byte array contains an ASCII or UTF8 string, then you would use the ASCIIEncoding, Utf7Encoding, or Utf8Encoding class to interpret the bytes into a string for you.
 
If you have the documentation for the file you were reading originally, then that documentation will tell you without a doubt how those original bytes were encoded.

If you don't have documentation, but you have the source code of what generated the file, then as we used to say many years ago: "Real code doesn't lie. Any existing documentation or comments are out of date."

If you don't have either documentation or source code, then all you can do is make an educated guess. Based on how that string looks there, as well as, your other question about casting, that suggests that the string is either ASCII or UTF-8 encoded assuming that the original code that generated the binary file was written in C or C++.
 
Is there a way to send you a private message with the link to the file? It's old but maybe still copyrighted and I think not suitable for this forum. I have no docs or source.
:(
And I also have a specific question about how to read something else from it (a non-string data spread across more bytes)

Please? ☺️
 
Sorry, I'll have to pass. Last time I tried to help someone with a proprietary file format file, she went all psycho on me. Just post here on the forum what you feel comfortable posting, and people can try to help you decipher the data.
 
That was me ... I have autism and had a meltdown (Meltdowns - a guide for all audiences) :(
I have a last question about the previous help you gave me. Maybe you want to help with that?

It's about finding out how a container format is made and I have 16 bit C code for it. But I have no experience with that :(

I hope you want to help with my last problem.
 
Last edited:

Latest posts

Back
Top Bottom