Click here to Skip to main content
15,910,121 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to read exactly byte, then seek until there is null byte, and write the seeked byte to a binary file.
VB
    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
            Else
                matchIndex = 0
            End If
        Next
        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
        fs.Close()
        '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
Updated 5-Feb-13 4:36am
v3
Comments
Sergey Alexandrovich Kryukov 4-Feb-13 21:26pm    
What did you try so far? Why it's the problem?
—SA
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

This is a Brute-force-and-ingorance approach, but...try:
VB
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
Next
startIndex += 1
Dim endIndex As Integer
For endIndex = startIndex To data.Length - 1
    If data(endIndex) = enddata Then
        Exit For
    End If
Next
extract = New Byte(endIndex - startIndex - 1) {}
Array.Copy(data, startIndex, extract, 0, endIndex - startIndex)
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900