Results 1 to 5 of 5

Thread: Reading a binary file?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Location
    Brighton, England
    Posts
    112

    Question Reading a binary file?

    Hi,
    I have a binary file which has a format consisting of an X value stored as a 32 bit signed integer, a Y value stored as a 32 bit signed integer and a 24 bit character array etc etc.

    My questions are:

    How do I read the data in and store the X, Y and character?

    How do I convert the X, Y and characters so I can use them in my program?

    I've read up a bit on binary files but I can't seem to get the data in and converted.

    Any help would be much appreciated.

    many thanks

    john

  2. #2
    Lively Member Flustor's Avatar
    Join Date
    Sep 2001
    Location
    A small hole in Birmingham
    Posts
    76

    Lightbulb

    A long way round would be reading this info through 4, 8 bit (256) lumps and then converting to binary, putting them together, converting them back so that they end up as 32 bit numbers.
    Sorry I can't do better...

    Flustor
    My Spidey senses are tingling!

  3. #3
    Tygur
    Guest

    Re: Reading a binary file?

    Originally posted by John Graham
    Hi,
    I have a binary file which has a format consisting of an X value stored as a 32 bit signed integer, a Y value stored as a 32 bit signed integer and a 24 bit character array etc etc.
    Do you mean a 24 byte character array?

  4. #4
    Tygur
    Guest
    Assuming you do, you can open the file as a random access file, which is easier:
    VB Code:
    1. 'This declares our data type used to access the file
    2. 'Put it in the top of the form (the General Declarations Area)
    3. 'You might want to give it a more meaningful name than "Info"
    4. Private Type Info
    5.     X As Long
    6.     Y As Long
    7.     Chars(1 To 24) As Byte
    8. End Type
    9.  
    10. 'This code will read all the data in the file and display the X and Y
    11. 'values, and also the text in the Character array on the form.  I
    12. 'don't think you will have much trouble uderstanding it and
    13. 'applying it for your app.  You might want to change it so it uses
    14. 'the FreeFile function instead of just using 1 for the file number.
    15. 'If you don't know how, just ask.
    16. Dim Rec As Info
    17. Dim I As Long
    18. Open "c:\testfile" For Random As #1 Len = 32
    19. Do
    20.     I = I + 1
    21.     Get #1, I, Rec
    22.     If EOF(1) Then
    23.         Exit Do
    24.     End If
    25.     Print "X = "; Rec.X; "; Y = "; Rec.Y; "; Chars = "; StrConv(Rec.Chars, vbUnicode)
    26. Loop
    27. Close #1

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Location
    Brighton, England
    Posts
    112
    thanks Tygur , I did mean a 24 byte character array, I'll try your code today, thanks again for the help

    john

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