-
Oct 19th, 2014, 09:13 AM
#1
Thread Starter
Addicted Member
file to text? not completely working
If i open a program/rar file/zip file with notepad or winword it shows all the ascii characters, etc... I tried to do this in vb and text files work, but not any of the others.. If I system.convert.tobase64string then it works, but ofcourse the size of the "file" is significantly larger.
how could I open the "file" and view the ascii text/etc. I have tried System.text.asciiencoding.ascii.getstring(my.computer.filesystem.readallbytes(FILE))...etc... I even tried the Windows-1252 enccoding...
it will pull the first couple of characters of the file thats it...unlless its a txt file. However if I use the base64 it works with all files...so it seems to be an "encoding" issue not compatible with textbox/richtextbox... not sure...
-
Oct 19th, 2014, 09:27 AM
#2
Re: file to text? not completely working
I think you'll find that Notepad replaces NULL characters with spaces, whereas your code is going to consider a NULL character the end of the text and not show anything that comes after. You can try something like this:
Code:
Dim text = File.ReadAllText(filePath).Replace(ControlChars.NullChar, " "c)
That will use the default encoding but you can provide a different Encoding object as a second argument to ReadAllText if you want, e.g.
Code:
Dim text = File.ReadAllText(filePath, Encoding.ASCII).Replace(ControlChars.NullChar, " "c)
-
Oct 19th, 2014, 10:55 AM
#3
Thread Starter
Addicted Member
Re: file to text? not completely working
thanks that worked... to visualize it.....
how would I reverse it now to get the file back...
i tried setting a string..
Code:
TextBox2.Text = System.Text.ASCIIEncoding.ASCII.GetString(My.Computer.FileSystem.ReadAllBytes(TextBox1.Text)).Replace(vbNullChar, "##N##")
to reverse
Code:
My.Computer.FileSystem.WriteAllBytes(System.IO.Path.GetDirectoryName(TextBox1.Text) & "\Test.rar", System.Text.ASCIIEncoding.ASCII.GetBytes(TextBox2.Text.Replace("##N##", vbNullChar)), False)
however this doesn't work.. i ge the same file size, but the file is broken/not complete..
-
Oct 19th, 2014, 05:32 PM
#4
Re: file to text? not completely working
I would imagine that the issue is the fact that .NET considers a String to end at the first NULL character, so your GetBytes call is only getting the Bytes for that String up to the first NULL character. If that's the case then a solution would be to write the data in chunks. You can use a loop to find each instance of "##N##" in the String, convert the substring up to that point, write that to the file followed by a NullChar and repeat until there's no text left.
-
Oct 19th, 2014, 06:16 PM
#5
Re: file to text? not completely working
Just converting bytes to respective ASCII characters isn't a good idea, because ASCII characters 0-31 are Special Characters and will cause problems. So, is best to convert byte to respective hexadecimal representation. You can do this:
Code:
Dim Data() As Byte = System.IO.File.ReadAllBytes("Input file you want to transform in text")
Dim Temp As New System.Text.StringBuilder()
For i As Integer = 0 To Data.Length - 1
Temp.Append(Hex(Data(i)).PadLeft(2, "0"c))
Next
Dim Text As String = Temp.ToString
'Text now contains your file
'Save text back to a file
Dim Out(Len(Text) / 2 - 1) As Byte
Dim Offset As Integer
For Position As Integer = 1 To Len(Text) Step 2
Out(Offset) = Val("&h" & Mid(Text, Position, 2))
Offset += 1
Next
System.IO.File.WriteAllBytes("Output file", Out)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|