Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I would like to convert a file of any type to ASCII and display it in a textbox. I Googled everywhere with no luck! :confused: Any assistance would be greatly appreciated!

This is what I am working on:

Dim myFile As New System.IO.FileInfo(C:\Test.exe)
Dim myStream As System.IO.FileStream = myFile.OpenRead()
Dim myFileLength As Long = myStream.Length
Dim myAscii(myLength - 1) As Byte

myStream.Read(myAscii, 0, myFileLength)
Dim A As Byte = Convert.ToString(myAscii(0))
TextBox1.Text = A

This only gives me 1 ASCII code; 23 for example. It does not keep reading the file to the end. I'm not even sure if this is the best method. I'm still learning! (If you've just read this question now, go through all the comments. You may find them a bit amusing (but not much!) because I completely missed them until now! Sorry again. It's just been one of those days.

And finally, after reading all the comments I'm editing my question, like I should have done a long time ago! I would like to make a loop to read ALL ASCII to the end of the file and display them in a textbox in the above code. Can anyone assist?
Posted
Updated 24-Aug-10 15:37pm
v3

So what you mean is that you want to read the byte data, and view it as a list of decimal numbers ? File.ReadAllBytes will give you a byte array, then you can use a string builder and foreach to iterate over that, and build your list of numbers. ASCII is the older way of encoding text, if you wanted hexadecimal representations of the data, then you'd be talking about hex, but as it stands, you just want to view the data. You CAN also show the ASCII representation of the data, most hex editors do that, but that would so a single character for each number, not the numbers themselves.
 
Share this answer
 
Your tags leave us with little idea of how you're doing this. What do you mean by 'ASCII' ? What do you mean by 'any type' ? How would you convert a bitmap, to text, for example ? A PDF ? You need to define business rules for what you mean, and unless you don't mean what you said, it sounds like a pretty complex task.
 
Share this answer
 
Comments
Yusuf 24-Aug-10 18:03pm    
Come one CG. You know better than this. Of course you can read a binary file bit by bit and convert each bit to ASCII like this

000110101010010010100 01010 0101010 1010101 01 1010001 1000010101111 111010 10101 011101 10 01000100

Sorry, can't hold back.
I appreciate the quick response. Thanks for the message to clarify. Sorry for being unclear. I'm new to VB.

Basically, I would like to convert a file (.jpeg, .mp3, .avi, .exe, etc.) to ASCII code.

Let's say an Mp3 for example.

I would to view it as: 65 122 205 36 101

Or even like you stated; read a binary file bit by bit and convert each bit to ASCII.

I've searched Google a million times and can't quite find what I'm looking for!
 
Share this answer
 
Comments
Christian Graus 24-Aug-10 19:55pm    
Please don't push answer to ask more questions, push comment or edit your post.
Dalek Dave 24-Aug-10 20:22pm    
Ongoing point is not to answer with another question, just respond with comment.
This is what I was working on; just the beginning.

Dim myFile As New System.IO.FileInfo(C:\Test.exe)
Dim myStream As System.IO.FileStream = myFile.OpenRead()
Dim myFileLength As Long = myStream.Length
Dim myAscii(myLength - 1) As Byte

myStream.Read(myAscii, 0, myFileLength)
Dim A As Byte = Convert.ToString(myAscii(0))
TextBox1.Text = A

This only gives me 1 ASCII code; 23 for example. It does not keep reading the file to the end. I'm not sure why either! I'm not even sure if this is the best method. I'm still learning!

File.ReadAllBytes sounds like what I'm looking for. Can you post a simple example of ReadAllBytes and display them in a textbox?
 
Share this answer
 
Comments
Dalek Dave 24-Aug-10 20:35pm    
Better that you do not answer your own question, do it as comments.
Christian Graus 24-Aug-10 21:15pm    
Like I said, do NOT push 'answer' to comment. You're reading myAscii(0). You want to read them all, a for loop or for each loop will let you do this.
Kevin James McGerk 24-Aug-10 21:26pm    
Doi! I just posted my first question today and already I keep messing up. First I wasn't clear and only I knew what the heck I was asking, and now I'm posting answers to my own question instead of comments! I didn't even realize I had responses to let me know this. I appreciate the correction. Sorry about that.

Can anyone assist with creating a loop to read all the ASCII to the end of file in the above code?
You don't have a book on VB.NET ? Writing loops is the most basic thing I can thing of after 'hello world'. I am happy to help, but it won't help you in the long run if you can't do basic research and/or have no resources. I even gave you the key words to type in to google.

Dim b as Byte

for each b in myAscii
 

end for

that gives you a loop, inside which you can add each byte to a string, preferably using a stringbuilder.
 
Share this answer
 
Comments
Kevin James McGerk 25-Aug-10 10:04am    
Dear Christian:

#1 I do have books on VB.net and used them to help me with the code above. Like I stated before, I'm new to VB.net and Googled it, but can't seem to find exactly what I'm looking for. (to put it more simply, the result I wanted was the entire ASCII of a file in a textbox, and whatever I tried didn't work.)

#2 A loop is not the most basic thing after hello world. However, there are many things more basic, like being helpful and sharing knowledge to new members and coders, and not making things difficult or being condescending like you.

#3 After my initial question, Yusuf knew exactly what I was talking about. Apparently, your code project awards listed below don't mean much.
1 Jan 2010: CodeProject MVP 2010
1 Jan 2009: CodeProject MVP 2009
1 Jan 2008: CodeProject MVP 2008
1 Jan 2007: CodeProject MVP 2007
1 Jan 2006: CodeProject MVP 2006
1 Jan 2005: CodeProject MVP 2005

#4 I was only being polite when I reworded my perfectly clear question to cater to you.

#5 Somehow I feel you're not really happy to help because thus far you've only been difficult and insulting; especially with your "Hello World" comment.

Thanks, but no. I don't need help from someone like you.

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