Home All Groups Group Topic Archive Search About

Extracting bytes from a file

Author
4 Jun 2009 12:00 PM
Hans Achim
Hello!

Does somebody know how I can extract a byte "section" of file, when I
know the byte start position and the length of bytes that should be
extracted?

Hans

Author
4 Jun 2009 12:17 PM
Nobody
"Hans Achim" <h.ac***@gmx.de> wrote in message
news:%23Sfx4xQ5JHA.1712@TK2MSFTNGP03.phx.gbl...
> Hello!
>
> Does somebody know how I can extract a byte "section" of file, when I know
> the byte start position and the length of bytes that should be extracted?

You could use Open/Seek/Get statements. Example:

Dim f As Integer
Dim b() As Byte

ReDim b(1 To 5) ' Get 5 Bytes

f = FreeFile
Open "C:\Test\Test.txt" For Binary As f
Seek f, 100 ' Start from the 100th byte(base 1)
Get f, , b
Close f

Use this hex editor to verify what you are doing:

http://www.chmaas.handshake.de/delphi/freeware/xvi32/xvi32.htm
Author
4 Jun 2009 2:09 PM
Larry Serflaten
"Hans Achim" <h.ac***@gmx.de> wrote
> Hello!
>
> Does somebody know how I can extract a byte "section" of file, when I
> know the byte start position and the length of bytes that should be
> extracted?

The Get command allows you to specify where to start reading:

<air code  warning...>
  ' Suppose you want 10 bytes from offset 200

Dim data(1 to 10) As Byte
Open "..." For Binary As 1
Get #1, 200, data
Close 1

The data array will contain 10 bytes,
from offset 200 to 209.  (The first byte of a file is position 1)

HTH
LFS