Results 1 to 11 of 11

Thread: [RESOLVED] Read file(binary) to string

  1. #1

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Resolved [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:
    1. Dim File As String
    2.  
    3.         Dim fs As FileStream
    4.         Dim binRead As BinaryReader
    5.         Dim readbuf(900) As Char
    6.  
    7.         Dim i As Integer
    8.         Dim strData As String = vbNullString
    9.  
    10.         File = "C:\UK Wholesalers.doc"
    11.  
    12.         fs = New FileStream(File, FileMode.Open, FileAccess.Read, FileShare.None)
    13.         binRead = New BinaryReader(fs)
    14.         readbuf = binRead.ReadChars(900)
    15.         binRead.Close()
    16.  
    17.         'convert the char array to string
    18.         For i = 0 To readbuf.GetUpperBound(0)
    19.             strData = strData & readbuf(i)
    20.         Next
    21.  
    22.         TextBox1.Text = strData

    anyone got any ideas?
    Chris

  2. #2
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    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:
    1. Private bin As IO.BinaryReader
    2.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3.         Dim intIN As Int32
    4.         Dim strOUT As String = String.Empty
    5.         Dim Rep As Int32
    6.  
    7.         Try
    8.             bin = New IO.BinaryReader(IO.File.OpenRead("C:\Sample.doc"))
    9.             intIN = bin.Read
    10.             Do Until Rep = 900
    11.                 If (Rep Mod 8 = 0 And Rep > 0) Then
    12.                     strOUT &= System.Environment.NewLine
    13.                 End If
    14.                 strOUT &= GetBinary(intIN) & " "
    15.                 Rep += 1
    16.                 intIN = bin.Read
    17.             Loop
    18.         Catch ex As Exception
    19.             'Handle
    20.         Finally
    21.             Me.TextBox1.Text = strOUT
    22.         End Try
    23.     End Sub
    24.  
    25.     Private Function GetBinary(ByVal inByte As Int32) As String
    26.         Dim dblLoop As Double = 256
    27.         Dim Out As String
    28.  
    29.         While dblLoop >= 1
    30.             If dblLoop > inByte Then
    31.                 Out &= "0"
    32.             Else
    33.                 Out &= "1"
    34.                 inByte -= Convert.ToInt32(dblLoop)
    35.             End If
    36.             dblLoop /= 2
    37.         End While
    38.  
    39.         Return Out

  3. #3

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    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
    Chris

  4. #4
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    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.

  5. #5

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    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
    Chris

  6. #6
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    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.

  7. #7

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    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:
    1. Open strFilename For Binary As #1
    2.     strBuffer = Space(LOF(1))
    3.     Get #1, , strBuffer
    4. 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
    Attached Images Attached Images  
    Chris

  8. #8
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Read file(binary) to string

    VB Code:
    1. Dim intIN As Int32
    2.         Dim strOUT As String = String.Empty
    3.         Dim Rep As Int32
    4.         Dim buff() As Byte
    5.  
    6.         Try
    7.             bin = New IO.BinaryReader(IO.File.OpenRead("C:\Client.exe"))
    8.  
    9.             intIN = bin.Read
    10.             While intIN >= 0
    11.                 If intIN <= 32 Then
    12.                     strOUT &= "."
    13.                 Else
    14.                     strOUT &= Chr(intIN)
    15.                 End If
    16.  
    17.                 Rep += 1
    18.                 If Rep Mod 16 = 0 Then
    19.                     strOUT &= System.Environment.NewLine
    20.                 End If
    21.  
    22.                 intIN = bin.Read
    23.             End While
    24.         Catch ex As Exception
    25.             '    'Handle
    26.         Finally
    27.             Me.TextBox1.Text = strOUT
    28.         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.
    Last edited by sevenhalo; Apr 6th, 2006 at 07:32 AM.

  9. #9

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    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
    Chris

  10. #10
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Read file(binary) to string

    VB Code:
    1. Dim strOUT As String = String.Empty
    2.         Dim buff() As Byte
    3.         Dim unibuff() As Byte
    4.         Dim uniByte(1) As Byte
    5.  
    6.         Try
    7.             bin = New IO.BinaryReader(IO.File.OpenRead("C:\NTDETECT.COM"))
    8.             buff = bin.ReadBytes(Convert.ToInt32(bin.BaseStream.Length))
    9.             unibuff = System.Text.Encoding.Convert(System.Text.Encoding.Default, System.Text.Encoding.Unicode, buff)
    10.  
    11.             For intI As Int32 = 0 To unibuff.Length - 1 Step 2
    12.                 uniByte(0) = unibuff(intI)
    13.                 uniByte(1) = unibuff(intI + 1)
    14.  
    15.                 If Asc(System.Text.Encoding.Unicode.GetChars(uniByte)) <= 32 Then
    16.                     'No character representation value, ignore and output "."
    17.                     strOUT &= "."c
    18.                 Else
    19.                     strOUT &= System.Text.Encoding.Unicode.GetString(uniByte)
    20.                 End If
    21.             Next
    22.         Catch ex As Exception
    23.             '    'Handle
    24.         Finally
    25.             Me.TextBox1.Text = strOUT
    26.         End Try

  11. #11

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: Read file(binary) to string

    bloody brilliant mate
    Chris

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width