Click here to Skip to main content
13,800,290 members
Rate this:
 
Please Sign up or sign in to vote.
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 29-Aug-18 15:43pm
Updated 31-Aug-18 8:37am
v5
Comments
Richard Deeming 30-Aug-18 10:20am
   
Sounds like you're looking for the ByteViewer[^] control.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
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[^]
Dave Kreskowiak 30-Aug-18 22:06pm
   
Again, no. YOU come up with the file format just by writing the data yourself.

There is no such thing as a "file format" that's defined by anything other than how an application writes it's data.
Dave Kreskowiak 30-Aug-18 23:09pm
   
As it is, you can't. At least not the way you're thinking. You don't have an integer or anything close to it. You have a large string made up of numeric characters.

You can't really easily just split up the string into a bunch of chunks that will fit into an integer. Converting the integers back would mean you also have to account for leading zero's to put back into the string.

You can use a GZip stream (compression) but this gets a bit complicated. For an example, see Compression/Decompression string with C# - Stack Overflow[^]
Richard MacCutchan 31-Aug-18 8:37am
   
While you are waiting patiently allow me to point out that everything that Dave has written is totally correct. Any file, however it is created, consists of a stream of bytes. The interpretation of those bytes into 'proper' data can only be done by the program that wrote them, or by someone who knows the exact format of the file. For example I have a data file for personal use whose first 10 bytes could look like this:

54 57 01 00 00 0C 16 00 00 00

The first two bytes represent a short word that identifies the file type, and its version. The next four bytes represent an integer with a control value in the top 8 bits, and a value of 1 in the low order 24 bits - it identifies the next field and its length.. The next four represent a word with a value of 22, which tells me how many records follow. I will refrain from trying to explain the format of each record, which contains encrypted text. All of these values are used by my program to create a list of records that I can display on screen in a ListView control. If you wrote a program to read that file you would have no idea how to interpret those values.
Dave Kreskowiak 31-Aug-18 10:02am
   
Dude. What's wrong with you?

Every time I say you really can't, or shouldn't, do something the way you want, you take it as an offense.

Yes, you ARE naive. You definitely "show a lack of experience, wisdom, or judgement" when it comes to this stuff. Why? Because you don't know this stuff as evidenced by the fact you're in a class for VB.NET! Your apparent problem is you don't seem to be able to admit "you don't know".

I'm trying to figure out what you're really doing from your use of the vaguest of terms and inaccuracies.

I'm trying to tell you how files really work and how there isn't really any such thing as a "file format" without an application that knows what every byte means in every position of the file.

I'm trying to tell you that the way YOU apparently DEMAND to do something isn't the best or easiest way to do it and what a better, more flexible alternative is.

...and what do you do? Accuse me of not "helping" you!

Sorry, but the length of this thread alone is testimony to the opposite.

Have fun with the remainder of your class.
Dave Kreskowiak 31-Aug-18 11:56am
   
First, 10^308 is a HUGE number and far higher than 8 bytes. There is no way you're storing 308 of anything in 8 bytes. Your 308 "bits" will fit in about 39 bytes, depending on storage format.

Second, you already have that string stored in a "compressed" format. It's the original file you built the "binary string" from.

Have a nice life.
Richard MacCutchan 31-Aug-18 12:57pm
   
10^308 is not 308 digits (something else you do not understand). It is 10 to the power of 308, and will generally be stored as a double length floating point value in 8 bytes. You really need to get hold of a decent book that will teach you the fundamentals of computer architecture, because there is not space here to provide the complete explanations that you need to learn about.
Member 13918845 31-Aug-18 13:12pm
   
Sorry, 10 to the power of 308 is 1 followed by 308 zeroes(digits) long and a double in visual basic can use all those digits without a decimal point and store in 8 bytes. Ya'll, there is no need to comment anymore. Really, do you just enjoy pointing out every error I make. Let's just end it here like grown men.
Nelek 31-Aug-18 13:59pm
   
You can provide screenshots... just do them, use a host service in internet like imgurg and copy here the link. Then you will be able to prove how wrong are the specialists you are asking help to.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.181214.1 | Last Updated 31 Aug 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100