Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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
            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 4-Feb-13 15:17pm
Edited 5-Feb-13 4:36am
v3
Comments
Sergey Alexandrovich Kryukov at 4-Feb-13 21:26pm
   
What did you try so far? Why it's the problem?
—SA
Leecherman at 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 at 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 at 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 at 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
good
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
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)
  Permalink  

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



Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 5 Feb 2013
Copyright © CodeProject, 1999-2014
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