[RESOLVED] Read file(binary) to string
been trying to do this for a few days with not much luck, I found some code when I searched but its not reading 900 chars of the file only the first few characters, heres my code:
BTW its supposed to read the first 900 chars
VB Code:
Dim File As String
Dim fs As FileStream
Dim binRead As BinaryReader
Dim readbuf(900) As Char
Dim i As Integer
Dim strData As String = vbNullString
File = "C:\UK Wholesalers.doc"
fs = New FileStream(File, FileMode.Open, FileAccess.Read, FileShare.None)
binRead = New BinaryReader(fs)
readbuf = binRead.ReadChars(900)
binRead.Close()
'convert the char array to string
For i = 0 To readbuf.GetUpperBound(0)
strData = strData & readbuf(i)
Next
TextBox1.Text = strData
anyone got any ideas? :)
Re: Read file(binary) to string
I think this is what you want... You just want to read the binary, not edit it, right?
I included formatting so it's not as hard to follow.
VB Code:
Private bin As IO.BinaryReader
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim intIN As Int32
Dim strOUT As String = String.Empty
Dim Rep As Int32
Try
bin = New IO.BinaryReader(IO.File.OpenRead("C:\Sample.doc"))
intIN = bin.Read
Do Until Rep = 900
If (Rep Mod 8 = 0 And Rep > 0) Then
strOUT &= System.Environment.NewLine
End If
strOUT &= GetBinary(intIN) & " "
Rep += 1
intIN = bin.Read
Loop
Catch ex As Exception
'Handle
Finally
Me.TextBox1.Text = strOUT
End Try
End Sub
Private Function GetBinary(ByVal inByte As Int32) As String
Dim dblLoop As Double = 256
Dim Out As String
While dblLoop >= 1
If dblLoop > inByte Then
Out &= "0"
Else
Out &= "1"
inByte -= Convert.ToInt32(dblLoop)
End If
dblLoop /= 2
End While
Return Out
Re: Read file(binary) to string
thanks sevenhalo but I meant read binary file as in not a .txt, I want to get the ascii text (not binary code) from the file which isnt a txt
Re: Read file(binary) to string
You lost me.
That snippet I gave reads a doc (MS word file, but works with any). If you just want the ASCII characters from it, get rid of the GetBinary part. It's passing the ascii value to the function and returning a string to represent it in binary. If you drop it, you'll get numerical data.
Re: Read file(binary) to string
sorry thats my fault for not explaining it well, okay what i want is the same as this....
when you open a .exe file in notepad it gives you the raw data as text for example:
Code:
Ÿj°ÛŸjs*jNb¬jÉfŸjÕh¬j'TŸjáKŸjb¬j§fŸjãBŸj,EŸj)/«jÚ«jšb¬j˜EŸjšc¬j2"«j®}ªjd³¬jßHŸj´«j¯DŸjõÀ«jÕcŸjoØžj¤Â«j&ž«jÓi«jæ»*jåi«jÁ'«jx«jŠi¬j%±*j…ãŸj ´¬j|gªj[NŸjÛœ¬jf¬jNc¬jØc«j*j|i«j¡²¬jÒªj‚XŸj¬Çžj`«jÎb¬jÎc¬jöDŸj d«j°HŸjc¬j=]¬jDQŸj>ÞjÊ*jÅ*jÜ*jSHŸj6±¬j^GŸjàJŸjq³«jh‘Ÿjê ¬jº´¬j5¬jPZ«j×PŸjþGŸjmÖªj
thats what i want to read into a string, and display in textbox, just as notepad does it :D
Re: Read file(binary) to string
Chris, you nerd... Your snippet used a .doc :)
I messed with this a little and it turns out Notepad cheats. If the value is EXT (End of Text) or EOT (End of Transmission), it replaces it with a space and continues. Which basically means Notepad turns your executibles into swiss cheese when you're looking at it. If you want to preserve the executible in its original state and read through it, you'll need to use the actual numeric values.
1 Attachment(s)
Re: Read file(binary) to string
yes I used a doc in my post #1 but by reading the fille for binary access it doesnt matter what file type it is, it should read the raw data, this is how I do it in VB6:
VB Code:
Open strFilename For Binary As #1
strBuffer = Space(LOF(1))
Get #1, , strBuffer
Close #1
and that would open up any filetype and put the raw data (like in my last post example data) into the strBuffer so it can be processed or just displayed.
the screenshot is a hex editor, it shows the file data in hex form, and in text form, thats what I want, to display the text like that in a textbox, i dont want to replace any chars, I know this hex editor replaces nothing with ..... but all i want is the original contents :confused:
Re: Read file(binary) to string
VB Code:
Dim intIN As Int32
Dim strOUT As String = String.Empty
Dim Rep As Int32
Dim buff() As Byte
Try
bin = New IO.BinaryReader(IO.File.OpenRead("C:\Client.exe"))
intIN = bin.Read
While intIN >= 0
If intIN <= 32 Then
strOUT &= "."
Else
strOUT &= Chr(intIN)
End If
Rep += 1
If Rep Mod 16 = 0 Then
strOUT &= System.Environment.NewLine
End If
intIN = bin.Read
End While
Catch ex As Exception
' 'Handle
Finally
Me.TextBox1.Text = strOUT
End Try
Change your textbox font to something like Lucinda Console or one of the other fonts that are character fixed width. It should look identical.
Re: Read file(binary) to string
sorry to keep annoying you but the code above reads some files, but not all? when it fails what happens is it just reads the first 4 or 5 chars only
Re: Read file(binary) to string
VB Code:
Dim strOUT As String = String.Empty
Dim buff() As Byte
Dim unibuff() As Byte
Dim uniByte(1) As Byte
Try
bin = New IO.BinaryReader(IO.File.OpenRead("C:\NTDETECT.COM"))
buff = bin.ReadBytes(Convert.ToInt32(bin.BaseStream.Length))
unibuff = System.Text.Encoding.Convert(System.Text.Encoding.Default, System.Text.Encoding.Unicode, buff)
For intI As Int32 = 0 To unibuff.Length - 1 Step 2
uniByte(0) = unibuff(intI)
uniByte(1) = unibuff(intI + 1)
If Asc(System.Text.Encoding.Unicode.GetChars(uniByte)) <= 32 Then
'No character representation value, ignore and output "."
strOUT &= "."c
Else
strOUT &= System.Text.Encoding.Unicode.GetString(uniByte)
End If
Next
Catch ex As Exception
' 'Handle
Finally
Me.TextBox1.Text = strOUT
End Try
Re: Read file(binary) to string
bloody brilliant mate:thumb: