Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looking at the binary

Status
Not open for further replies.

Pumices

Programmer
Dec 1, 2004
16
US
ok, how would I look take a file... any file and look at the binary code in vb? Everything exist in binary atleast on the harddrive if nothing else and I just need to access the raw binary data. Is there anyway I can do this? Thanks
 
Umm, use the BinaryReader class?
Code:
Dim binReader As New BinaryReader(File.Open(fileName, FileMode.Open))
Try
   Dim b As Byte
   While(True)
      b = binReader.ReadByte
      ' Do something with the byte
   Wend
Catch ex As EndOfStreamException
End Try
Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
That is close to what I needed but it is returning numbers other than 0 and 1 it is returning numbers like 80, 124, 86, etc. What I want to be able to do is take the binary from a large file and save the binary into a text file as quickly as possible. I want to then be able to reconstruct the file using that binary code at a later time. Thank you for your help so far and I hope this helps to better explain my problem. Thank you again
 
If you want to see the binary representation of the bytes you could use something like this:

Code:
    Dim MyByte As Byte
    Dim s As String = ""
    Dim bitset As Boolean
    For a As Integer = 7 To 0 Step -1
      bitset = (MyByte And CByte(Math.Pow(2, a))) = CByte(Math.Pow(2, a))
      If bitset Then
        s += "1"
      Else
        s += "0"
      End If

Hope this helps.
 
Yes, that does help and thank you but just one more thing. If I save the binary to a text file. How would I reverse that process to recreate the file and if I did would the file be unaltered. Thank you so much for your help you guys have been incredibly helpful up to this point.
 
One problem - the string will have the value in "reverse" order:

"00000001" = 1
"10000000" = 128

As your text file size should be an exact multiple of eight - there are a number of options, however (the slowest) but easiest would be to read the file one byte at a time:

Each byte represents the ASCII representation of a bit i.e.

48 = "0"
49 = "1"

you will need to maintain a position index (from 7 to 0) and a byte variable

typed not tested

Code:
Dim ResultByte As Byte
Dim StringBit As Byte

'assumes the following is within a while not end of file loop

ResultByte = 0
For a As Integer = 7 To 0 Step -1
  'read a byte from input file into StringBit
  StringBit -=48
  ResultByte +=  StringBit * CByte(Math.Pow(2,a))
Next a
'write the byte ResultByte to the output file

At the end of this the ouptput file should be the same as the original input file

Hope this helps.

 
Thank you very much that was exactly what I needed to know. Thank you for all the help you have made my day... or night.
 
That is close to what I needed but it is returning numbers other than 0 and 1 it is returning numbers like 80, 124, 86, etc.

Well, yeah. They are byte values, after all. Anything between 0..255 is valid, and is a correct representation of what is stored on disk.

Most people don't care about the actual bit patterns, as they can be gotten via simple powers-of-two manipulations like earthandfire did.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top