Solved

Read File Bit By Bit

Posted on 2001-07-19
22
546 Views
Last Modified: 2010-08-05
I have a file that has columns of data.  I don't yet understand the file format allthough I do have a document that tells me what is contained in each column.  I must write a routine to display this data as ascii text.  In order to read this file, I want to start at the beginning of the file and determine each bit as a 1 or 0.  It appears that the format could be something like this.
110 'first three bits describe the length of
    'the next field. etc...  

When I figure out the structure, it should be possible to read the next n bits and convert to ascii and display.

Seems the place to start is to look at the file as bits.

I have viewed the file with a hex editor, but the hex and hex to ascii makes no sense.  Perhaps as in the 110 example above, the hex editor does not know where to start and stop reading, therefore gibberish.

I can make a RichTextBox to contain the 1's and 0's, this will help figure out the structure based on my discription document, but I have not been successful in reading the file this way.  How to do it?

Mitch......
0
Comment
Question by:MitchSavage
  • 9
  • 5
  • 5
  • +1
22 Comments
 

Expert Comment

by:jchew
ID: 6301187
' this is what is usually use
Private Function Char2Hex(ByVal sChar$) As String
   Dim iAscVal

   iAscVal = Asc(sChar)
   Char2Hex = Right("00" & Hex(iAscVal), 2)
End Function

Sub OpenFileDebug(Filename as String, Txt As TextBox)
   Dim iFile As Integer
   Dim sBuf  As String
   Dim iLen  As Integer
   Dim iCtr  As Integer

   On Error Goto ErrTrap
   iFile = FreeFile
   Open Filename For Input As #iFile
   Do While Not Eof(iFile)
      Line Input #iFile, sBuf
      iLen = Len(sBuf)
      For iCtr = 1 To iLen
         Txt.Text = Txt.Text & Char2Hex(Mid(sBuf, iCtr, 1)) & " "
      Next
      Txt.Text = Txt.Text & vbCrlf
   Loop
   Close #iFile
   Exit Sub

ErrTrap:
   MsgBox Err.Description
   
End Sub


---

you can easily convert from Hex to binary easily...  i'll try to search for my Char2Bin conversion function... wait!

hope this helps.
0
 

Expert Comment

by:jchew
ID: 6301198
ok, found it!

this is my character to binary conversion function. call this instead of Char2Hex to get the binary output


---

Function Char2Bin(ByVal sChar$) As String
   Dim iAscVal As Integer
   Dim sOut    As String
   Dim iBal    As Integer
   Dim iCtr    As Integer
   Dim aVal()  As Integer
   Dim iDiv    As Integer
   
   iAscVal = Asc(sChar)
   iDiv = 1
   For iCtr = 1 To 8
      If (iAscVal And iDiv) = 0 Then
         sOut = "0" & sOut
      Else
         sOut = "1" & sOut
      End If
      iDiv = iDiv * 2
   Next

   Char2Bin = Left(sOut, 4) & "-" & Right(sOut, 4)
End Function


0
 

Author Comment

by:MitchSavage
ID: 6301210
jchew...  This looks good so far.  I'll try this in code in the next 12 hours.  It is 2AM here so I must sleep soon. I'll try this and post you a comment...
Thanks and
Best Regards,
Mitch......
0
 
LVL 3

Expert Comment

by:leojl
ID: 6301424
hi mitch

if the data is ascii then there must be an easy way to read the file and write the file or whatever. it is not common practice to try to look at bits and then convert to ascii..

if you have Microsoft Excel you can try to import it as a text file and Excel will do it for you.

give us an example file to look at here, but if that is not possiple then send a file to me as an attachment to an email...send to leojl@yahoo.com

leo

0
 

Expert Comment

by:jchew
ID: 6301566
i've seen such files before. it's normally used when a record has many fields but the fields are optional.  so to minimize the data, at the begining of the record, there's a few bytes of data to state which fields are in the record.  

each bit represents a field, if it's 1, the field is present, if 0, the field is not.
0
 

Author Comment

by:MitchSavage
ID: 6303423
OK, NEWS FLASH....    The first 159 Bytes are the file header info.  The data I want begins at Byte 160 and continues for the next 159 bytes.  Next record begins at Byte 320 and continues for 159 bytes, etc.. to the EOF.
The confusing issue is....  Some of the bytes such as byte 1 in the record is hex 00 or 1A or 39 or 51. Byte 1 is very easy because of the four hex choices mean something significant, so no problem. Next is a 2 byte field that can be from hex 0000 to hex 3FFF, again no problem. Next is a 4 byte field, but here I have to look at which bits are set, bit 0 a 1 indicates yes or a 0 indicates no, bit 1 a 1 indicates yes or a 0 indicates no  etc.  So I suppose on this field I can convert Hex to Bin and examine each bit for the 1 or 0 condition. I am placing the output data into a grid that shows ASCII characters.  I know how to do all that, its all this Hex Bin ASCII reading and converting that I don't fully understand.

It appears that I need to open the file and begin at byte 160 and get 159 Hex characters.  Then depending on the byte position, do hex to ascii and place into my grid, or do hex to bin then examine each bit and depending on condition write "yes" or "no" to my grid. And, of course continue and analyze the bytes and or bits until end of record, then load next record, etc. to EOF.  With you experts helping me I actually think I/WE can do this. <Big Grin>....  Anyone have any new ideas based on this info?
Thanks a lot
Mitch
0
 

Author Comment

by:MitchSavage
ID: 6303925
ichew....  Do you have a HexToBin routine and a HexToASCII routine?  I have been studying this file for 2 days.  It appears that I might be able to do this read and convert if those two routines are available. At least I'm making progress.  I almost understand the file format now that I can view it and work with it.
Thanks, Regards....
Mitch.....
0
 
LVL 8

Expert Comment

by:glass_cookie
ID: 6304940
Hi!

Here's a file over the net for you:

Download...
http://www.vb-helper.com/Howto/filepos.zip

Description: Read bytes from a specific part of a file (3K)

That's it!

glass cookie : )
0
 

Author Comment

by:MitchSavage
ID: 6305172
Hello glass cookie.....  The vb-helper site will not let me d/l any files right now.  I'll try later to get the file. Nice to hear from you again....
Best Regards,
Mitch
0
 
LVL 3

Expert Comment

by:leojl
ID: 6305225
hi

try vbhelper without he -

leo
0
 
LVL 8

Expert Comment

by:glass_cookie
ID: 6305952
Hi!

I suppose the site is down.

I managed to get the link only through some other means (from indirect links from the page).

Since you've shown some interest in the link, here's some description of the example program.

You'll have to enter/specift the start byte position and the stop byte position.  Then, the example would give you the bytes within that range and display it in a label.

That's it!

glass cookie : )

PS. I guess you'll have to wait.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:MitchSavage
ID: 6306435
glass cookie....    do you have the filepos.zip file?  It appears that the file server at vb-helper is down....
Later today I am posting code here that I am using.  I would like to award points on this question and move to the next one.
Best Regards
Mitch.....
0
 

Author Comment

by:MitchSavage
ID: 6306557
Here is what I am using now to read the file and display one record in hex.  If anyone has any comments or suggestions, great.....  This question has evolved into an entirely different question, so I will award points in 24 hours and post a new question for the next phase.  Thanks everyone very much for your help.

The following code is simple, but it does seem to work.
The form has 1 Rich Text Box to show the output, 1 text box to input the record number and 1 command button to start the process.
--------------------------------------------------------

Private Sub cmdGetRecord_Click()
   
    Dim RecFromFile As String
    RecFromFile = String(159, " ")
   
    Dim FileNumber As Integer
    FileNumber = FreeFile
   
    If txtFile.Text = "" Then MsgBox "File not found.": Exit Sub
   
    Open txtFile.Text For Binary As FileNumber
    Get FileNumber, (txtGetRecord * 160) + 1, RecFromFile
    Close FileNumber
         
    Dim DisplayThis As String
    Dim HexOut As String
    Dim k As Long
    Dim i As Long
    Dim j As Long
    Dim l As Long
    rtbHex.Text = ""
   
     For k = 1 To Len(RecFromFile) Step 16
       
        For i = k To k + 15
            HexOut = "00"
               If i <= Len(RecFromFile) Then
                  j = Asc(Mid$(RecFromFile, i, 1))
                  HexOut = Hex$(j)
                    If Len(HexOut) < 2 Then
                       HexOut = "0" & HexOut
                    End If
               End If
           
            DisplayThis = DisplayThis & HexOut & " "
        Next
       
        rtbHex.SelText = DisplayThis
        DisplayThis = ""
   
    Next
   
End Sub
0
 
LVL 8

Expert Comment

by:glass_cookie
ID: 6307259
Hi!

I actually downloaded a copy of that proggy, but it's at home.  So, you'll have to wait until I get home - about 6 hours from now.  Meanwhile, please state your email address so that I can email you the stuff : )

That's it!

glass cookie : )
0
 
LVL 8

Expert Comment

by:glass_cookie
ID: 6307260
FYI, I've gt no idea whether it's exactcly the same as your above code but I'll email you the stuff, just in case it's different : )
0
 
LVL 3

Expert Comment

by:leojl
ID: 6307308
hi glass cookie,

I have a sort of vision of the glass cookie girl in my mind. Would I be too bold to ask if you have a picture or would it be better to continue this quite impressive mental thing ?

sincerely
leo
0
 

Expert Comment

by:jchew
ID: 6307375
hello mitchsavage,

just got back from my weekend...  here's the hex2lng function.


'
' convert hexadecimal to long integer
' - no overflow checking
Function Hex2Lng(ByVal sHex$) As Long
Const cHEXCHAR = "0123456789ABCDEF"
Dim iPos    As Integer
Dim iLen    As Integer
Dim iCtr    As Integer
Dim lRet    As Long

   iLen = Len(sHex)
   For iCtr = 1 To iLen
      iPos = InStr(1, cHEXCHAR, Mid(sHex, iCtr, 1), vbTextCompare)
      lRet = (lRet * 16) + (iPos - 1)
   Next
   
   Hex2Lng = lRet
End Function

0
 
LVL 8

Expert Comment

by:glass_cookie
ID: 6307664
Hi leoji!

If you don't mind, could you elaborate on the "quite impressive mental thing" that you're talking about?  I don't quite get you : )

Thanks!

Oh yes, MitchSavage, the code that I proposed only returns the characters.  Do you still want it?

Alternatively, here's another example (which should be downloadable):

View code...
http://www.planetsourcecode.com/xq/ASP/txtCodeId.2903/lngWId.1/qx/vb/scripts/ShowCode.htm

Description: This module allows you to view a file as a collection of bits rather than as a collection of bytes. It allows you to read/write a single bit at a time or read/write up to 32 bits at once.

AND This one:

Download...
http://www.planetsourcecode.com/upload/ftp/CODE_UPLOAD6494662000.zip
Description: Allows you to open and view small binary files. For each byte in the file, you see the position, ASCII character (if it's printable) and the numeric value and its hexadecimal equivalent. Nothing fancy, but a quick way to see what's under the hood.

Snapshot page: http://www.planetsourcecode.com/xq/ASP/txtCodeId.8680/lngWId.1/qx/vb/scripts/ShowCode.htm

This one looks as if it reads the entire thing for you.  It splits a file into a long string and returns all the values.

AND The last one:

Download...
http://www.planetsourcecode.com/upload/ftp/CODE_UPLOAD1263912132000.zip

Description: Display the contents of any file in Hex/Ascii format. Code is well-documented and demonstrates uses of the Status Bar, Progress Bar & Rich Text Box controls. Updated program on 12/13/2000: Added a C Dll that greatly increased format speed of input file. Check HexView.cpp for a tip on how to debug a dll called from Visual Basic. I included Visual Basic statements in the HexView.cpp file so it serves as a tutorial for someone interested in writing a C Dll.

Snapshot page: http://www.planetsourcecode.com/xq/ASP/txtCodeId.13432/lngWId.1/qx/vb/scripts/ShowCode.htm

That's it!

glass cookie : )

PS. By the way, I'm not a girl.  Does "glass cookie" sound unisex or does it sound as if it's a girl?
0
 

Author Comment

by:MitchSavage
ID: 6307734
Thanks jchew for the Hex2Lng, I'm sure I will use it in the app. Do you have a Lng2Bin? That is the last missing piece in the puzzle.

And yes glass cookie I'm still interested in the code.
My email is mitchsavage@home.com  so fire away.....

My code above does work for extracting some of the data I need.  It is DREADFULLY slow.  I have wrapped a loop around it to get 1000 records, and the string stuff must be really dragging it down.  I'll post another question concerning that when I get it all put together.

Best Regards
Mitch Savage.....
0
 

Author Comment

by:MitchSavage
ID: 6307766
A little about my app.  I have used this app for about 2 years to read records from industrial machines.  They usually output simple ascii text with a delimiter.  These are easy to read, I just extract the data I want between the delimiters and place into a flexgrid column.  The records consist of the date, starttime, stoptime, controlnumber, sourcenumber, outnumber, sequencenumber, etc.  My prog does some math such as the difference between starttime and stoptime and displays the time diff in a new column. The TotalHours are calculated, the runtime hours are calculated, etc.....  This new machine does not output ascii.  It outputs these "binary" files.  I must import the files from the new machine and convert the output to look exactly like the output from all the other machines.  So, I'm trying to import the binary record into my app and extract the same data we have come to expect......   Making progress.......    
Regards to all....
Mitch Savage.....
0
 

Accepted Solution

by:
jchew earned 100 total points
ID: 6308019
hi,

this is my Lng2Bin function.



Function Lng2Bin(ByVal lVal As Long) As String
Dim iRem    As Integer
Dim sOut    As String

   Do
      iRem = lVal Mod 2
      lVal = lVal \ 2
     
      If iRem = 0 Then
         sOut = "0" & sOut
      Else
         sOut = "1" & sOut
      End If
     
      '
      ' add a space after every 4 bits to improve readability
      If ((Len(sOut) + 1) Mod 5) = 0 Then
         sOut = " " & sOut
      End If
   Loop Until lVal = 0
 
  Lng2Bin = sOut
End Function

0
 

Author Comment

by:MitchSavage
ID: 6309296
Thanks jchew for all the great help.  I have come a long way since posting the original question. You have responded with great answers and I do appreciate it.  I am putting together the code I am using now to read the file and starting a new question.
Thanks everyone else for the comments.  
Best Regards,
Mitch Savage....
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now