Results 1 to 5 of 5

Thread: getting info from binary file

  1. #1

    getting info from binary file

    I've been trying to write a routine to get information from a binary file and so far can't get it to work.

    This is what I've been trying:

    [code=thinbasic]
    uses "CONSOLE"
    uses "FILE"

    dim tempval as long
    dim i as integer
    dim some as byte
    dim gFile as string
    gFile = "test"

    tempval=file_open( gFile ,"BINARY")
    for i=1 to 7
    some = file_get(tempval, 1)

    console_write(mkbyt$(some))
    console_writeline("")
    next

    file_close(tempval)
    [/code]

    I think it's obvious that I don't know what I'm doing. Can someone clue me in on what I'm doing wrong? It doesn't get anything from the file.

  2. #2

    Re: getting info from binary file

    Ok, I'm just guessing here. The filename has no path and extention. Are you sure it found it? Check it's return value.
    For the rest I have to study thinBasic a little more. Sorry.

  3. #3
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    Re: getting info from binary file

    Hi Sandy,

    I'm not in office now, so I will check later but there should be an error in help file about FILE_GET function. It should return a string on n bytes and not a number. I will fix help file for next release.

    See this example.
    Ciao
    Eros

    [code=thinbasic]uses "CONSOLE"
    uses "FILE"

    dim fHandle as long
    dim i as integer
    dim some as string
    dim gFile as string

    '---Set file name to open
    gFile = app_path & "thinbasic.exe"

    '---Open file
    fHandle = file_open(gFile, "BINARY")

    '---Get first 20 bytes ...
    for i=1 to 20

    '---...each byte. FILE_GET returns a string composed by the number of bytes
    '--- has been specified to be taken from the open file handle
    some = file_get(fHandle, 1)

    '---Write out ascii code and char only if printable
    console_writeline format$(asc(some), "000") & " " & iif$(asc(some) > 32, Some, "")

    next

    '---Close file
    file_close(fHandle)

    '---Wait for user to press a key
    console_waitkey
    [/code]
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  4. #4
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10

    Re: getting info from binary file

    Sandy,

    I had some time to implement your example and add comments.
    Here it is the code and also as attached file below
    Hope it will help you.

    Ciao
    Eros

    [code=thinbasic]
    '------------------------------------------------------------------
    ' Print out any file on screen in HEX and text format
    '
    ' This script will read a file from disk and will print
    ' on screen its hex decimal representation. sBufferLen bytes
    ' of data will be loaded for each loop
    '------------------------------------------------------------------

    '---Declare modules to be used in this script
    uses "CONSOLE"
    uses "FILE"

    '---Define needed variables
    dim fHandle as long '---File handle
    dim Counter as long '---Used to count bytes
    dim Looper as long '---Used to loop into string buffer bytes
    dim sBuffer as string '---Will store read data from file
    dim sChar as string '---Used for single byte tokenizing
    dim sLine as string '---Used for line our composing
    dim gFile as string '---File name to be loaded
    dim sBufferLen as long value 16 '---Number of bytes to be read for every line loop

    '---The following data is used for string replace in order to output
    '---binary strings on screen.
    dim ReplaceBinIn as string value chr$(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31)
    dim ReplaceBinOut as string value "................................"

    '---Start timers
    dim T0, T1 as double = timer

    '---Set file name to open
    gFile = app_path & "thinbasic.exe"

    '---Open file
    fHandle = file_open(gFile, "BINARY")

    '---While not at end of file ...
    while FILE_EOF(fHandle) = %FALSE

    '---Write byte counter in multiple of sBufferLen
    console_write format$(Counter, "00000000") & " "

    '---Load sBufferLen bytes in sBuffer
    sBuffer = file_get(fHandle, sBufferLen)

    '---Increment Counter by sBufferLen
    Counter += sBufferLen

    '---Now loops for all sBufferLen bytes in order to print HEX values
    sLine = ""
    for Looper = 1 to sBufferLen
    '---Get single byte
    sChar = mid$(sBuffer, Looper, 1)
    '---Write out ascii code in hex format
    sLine += hex$(asc(sChar), 2) & " "
    next
    console_write sLine

    '---Now we print out sBufferLen buffer but before to be able to print
    '---we have to change ascii chars from 0 to 31 with something printable
    sBuffer = replace$(sBuffer, any ReplaceBinIn, ReplaceBinOut)

    '---Now we can print it and go next line
    console_writeline sBuffer

    wend

    '---Close file
    file_close(fHandle)

    '---Set finish time
    T1 = timer

    '---Output some info
    console_writeline string$(79,"-")
    console_writeline "File analized: " & gFile
    console_writeline "File size: " & file_size(gFile) & " bytes"
    console_writeline "Operation performed in " & format$(T1 - T0, "#0.0000") & " seconds."

    '---Wait for user to press a key
    console_waitkey
    [/code]
    Attached Files Attached Files
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  5. #5
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    67
    Posts
    3,869
    Rep Power
    404

    Re: getting info from binary file

    Wow beautiful looking code Eros and cool example, thanks!
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

Similar Threads

  1. decimal <--> binary
    By danbaron in forum Science
    Replies: 0
    Last Post: 31-05-2011, 07:33
  2. Binary Data
    By Michael Clease in forum thinBasic General
    Replies: 20
    Last Post: 12-06-2008, 09:02

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

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