Click here to Skip to main content
13,052,433 members (36,615 online)
Rate this:
Please Sign up or sign in to vote.
See more:
How to read exactly byte, then seek until there is null byte, and write the seeked byte to a binary file.
    Public Function FindBytes(ByVal src As Byte(), ByVal find As Byte()) As Integer
        Dim index As Integer = -1
        Dim matchIndex As Integer = 0
        ' handle the complete source array
        For i As Integer = 0 To src.Length - 1
            If src(i) = find(matchIndex) Then
                If matchIndex = (find.Length - 1) Then
                    index = i - matchIndex
                    Exit For
                End If
                matchIndex += 1
                matchIndex = 0
            End If
        Return index
    End Function
    Private Function GetBytes(ByVal filename As String) As Byte()
        Dim fs As New IO.FileStream(filename, IO.FileMode.Open, IO.FileAccess.Read)
        ' Create a byte array of file stream length
        Dim ImageData As Byte() = New Byte(fs.Length - 1) {}
        'Read block of bytes from stream into the byte array
        fs.Read(ImageData, 0, System.Convert.ToInt64(fs.Length))
        'Close the File Stream
        'return the byte data
        Return ImageData
    End Function
Dim PNGStart As Byte() = {&H89, &H50, &H4E, &H47} ' PNG HEADER
Dim PNGEnd As Byte() = { &H49, &H45, &H4E, &H44, &HAE, &H42, &H60, &H82}
Dim StartPosition As Integer = FindBytes(GetBytes(filename), PNGStart)
SO How to Loop throw PNGStart to PNGEnd and write it to a file?

Note: I have found FindBytes function in other site, but I don't remember it's name, credits to the one who created it.
Posted 4-Feb-13 15:17pm
Updated 5-Feb-13 4:36am
What did you try so far? Why it's the problem?
Leecherman 4-Feb-13 22:05pm
I'm trying to read specie bytes from file, then after finding it, i will make the start position from the founded bytes, then seek and writing to binary file until null bytes is found.
tiggerc 5-Feb-13 10:01am
do you have some code?
I would suggest reading the bytes into an array, looping through the array building another list of bytes until the byte you are at is null( 00 ) then writing your built list to a file.

then continue the process until you have read all bytes and created all the files you want.

Have a go at it, we will help, but you need to at least show some of your code.
Leecherman 5-Feb-13 10:14am
No problem while getting bytes from file, but the problem is when finding the exact byte, and loop until there is null byte.
Leecherman 5-Feb-13 11:18am
thanks, but this doesn't accept {&H89, &H50, &H4E, &H47}
it only accept one byte!

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This is a Brute-force-and-ingorance approach, but...try:
Dim path As String = "D:\Temp\MyPic.jpg"
Dim startdata As Byte = &Hff
Dim enddata As Byte = &H0
Dim data As Byte() = File.ReadAllBytes(path)
Dim extract As Byte()
Dim startIndex As Integer
For startIndex = 0 To data.Length - 1
    If data(startIndex) = startdata Then
        Exit For
    End If
startIndex += 1
Dim endIndex As Integer
For endIndex = startIndex To data.Length - 1
    If data(endIndex) = enddata Then
        Exit For
    End If
extract = New Byte(endIndex - startIndex - 1) {}
Array.Copy(data, startIndex, extract, 0, endIndex - startIndex)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 5 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100