Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
I need to open any type file and convert it to binary then display that binary to either a text-box or listbox. I know about the imports IO and the openDialogBox and I've seen seen someone use a text-box to enter text and then display its binary. How would I accomplish this.

What I have tried:

I have used the open dialog box to open a .bin file type already and displayed it in a text box but it was encoded.
Posted
Updated 31-Aug-18 7:37am
v5
Comments
Richard Deeming 30-Aug-18 10:20am    
Sounds like you're looking for the ByteViewer[^] control.

1 solution

Your question shows a ton of naivety.

All files are already "binary". So you're going to have to explain what you mean by this. Are you looking to show the content in a hex-editor format, with the offset and byte value at that offset along with a text representation of that byte?

.BIN files are just data, more than likely never text, so showing that content in textbox is going to show nothing but what appears to be random garbage.
 
Share this answer
 
Comments
Dave Kreskowiak 30-Aug-18 8:43am    
Yeah, you didn't clear up any of the naivety. There is no such thing as "converted back when opened". They stay "binary", which is just a stream of bytes. How a file is read is either going to be in "binary" mode, read as a stream of bytes, or as a text stream,, where the stream of bytes is interpreted as text depending on an encoding used. Text can be multiple bytes per character.

Applications that are reading data typically are reading the file as "binary" but deserializing that data into data structures in the application. THAT is when the data in the file becomes meaningful and "readable". The application knows how to take that data and render it on screen as readable information.

A .bin file doesn't have any meaning or format. It's just a stream of bytes. You can pick any file you want and simply change the file extension and it's the exact same file. I have no idea what your talking about when you say "pick a file and convert it to .bin". That file only has meaning to the script that created it and only that same script, or whatever is setup to open .bin files by default on your machine, has any clue how to convert that file back.

It sounds as though that script you have may be a compression utility that compresses files into smaller archives. When it's "converted back" a utility is decompressing the file back to its original content so the application assigned to reading that file type is then able to read it again.

Displaying a file "as the computer sees it" is just a stream of bytes, some of it may represent text, but most will be non-human readable, seemingly random garbage.

If you're talking about how the application reads and renders the content of the file on screen, what you're talking about is impossible. Only the application that reads those file types is going to be able to make sense of the file, interpret the content, and render it correctly on screen.

You can write your own interpreters and rendering code to do this, but you're going to have to write these things for EVERY FILE TYPE YOU WANT TO SUPPORT. There is no such thing as writing this once and it works for every file type! That is simply impossible.


Dave Kreskowiak 30-Aug-18 11:14am    
YOU are wasting your precious time. You don't seem to understand that files are seldom text-based and human readable.

Not every byte of a file represents a text character. There are 256 possible byte values, only a portion of which represent printable characters.
Dave Kreskowiak 30-Aug-18 13:29pm    
Hey, I tried to get a more specific description of what you're trying to do out of you and pointed out how your description of how files worked wasn't accurate at all, and improve your understanding.

So far, my best guess still stands. You're trying to write something like a hex editor that shows file content byte-by-byte, but you did't say anything about it.

Richard posted a comment that is making the same assumption, and you didn't say
anything about it.

If you can't describe what you want to do to a bunch of humans, how are you going to describe how to do it to a computer that is far more picky?

Dave Kreskowiak 30-Aug-18 14:47pm    
I would like to read the binary of the file and display the binary aka 0's and 1's to a listbox or textbox.
FINALLY, a concrete spec we can work with!

The BinaryReader will return bytes, not an array of 0's and 1's. Technically, you don't even need it because there's a shortcut, File.ReadAllBytes(). Use the shortcut and you get back an array of Byte.

Now, for every byte, you have to build a string of 0's and 1's yourself. For that, you use a StringBuilder object. You also have to go through each byte and determine if each bit is on or off. That takes a little bit masking and an And operator.

You'll need a loop to go through every byte in the file and another inner loop to go through all 8 bits in each byte. If the masked off bit comes back a 0, tell the StringBuild to append a "0", otherwise append a "1".

Careful. The TextBox really wasn't designed to show megabytes of text. If you open a 1MB file to generate this string of 1's and 0's, you'll end up with a string 8MB in length.
Dave Kreskowiak 30-Aug-18 21:46pm    
The file extension does not dictate what the file format is. The application writing the file and reading it back is what determines the format and it has complete complete control over what it write and how. That's what makes the format.

The file extension on a file is like a license plate on a car. Changing the plate doesn't change anything at all about the car, does it? The same is true for file extensions.

You can give your file any extension you want. It doesn't matter.

For an example of how to write the data using a BinaryWriter and read it back with a BinaryReader, see BinaryWriter Class (System.IO) | Microsoft Docs[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900