Click here to Skip to main content
15,891,597 members

How to find exactly byte, then seek until there is null byte and write to a binary file.

Leecherman asked:

Open original thread
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.
Tags: Visual Basic

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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