Phillip Proctor
Phillip Proctor

Reputation: 59

Visual Basic string from null terminated byte array

Here is what has happened up until the point I am stuck:

  1. A file was selected and then read into a byte array using System.IO.File.ReadAllBytes(filename)

  2. The byte array contains compressed data which is decompressed and again stored in a byte array

  3. The now uncompressed data contains a block of text which appears to be null terminated. substituting a "." as &H0 it would looks something like"

Some random text.more random text.even more random text.

Part of the data in the file does give me the starting locations of each block. (for example 0,17,34 etc). The c source code that read's this file seems to be able to handle just having the starting location in the array of the data and automagically grabs everything from that location up to (and maybe including?) the &H0 value.

I am trying to figure out how to do this in visual basic. I have searched around and the closest thing I could find basically ends up being something like:

MyString = System.Text.Encoding.ASCII.GetString(DataBlock, y, Array.IndexOf(DataBlock, CType(0, [Byte])))

however that does not work. This is the exception I get:

System.ArgumentOutOfRangeException was unhandled HResult=-2146233086 Message=Index and count must refer to a location within the buffer. Parameter name: bytes ParamName=bytes Source=mscorlib

I have tried various ways of trying to specify the "0" however they all end up with an error. For instance changing it to "chr(0)" gives me

"Non-negative number required. Parameter name: byteCount"

So long story short (too late) how would one efficiently get a string of text that is delimited with &H0 from a byte array? (preferably without using for/next loops)

Update:
For starters I had some errors. Y was getting it's info from "data" which was probably defaulting to the classes .data instead of from "datablock" which is the decompressed data. 2nd I needed to offset the y value by where the text data starts. So THIS code:

Console.WriteLine(System.Text.Encoding.ASCII.GetString(DataBlock, BlockData + y, Array.IndexOf(DataBlock, CType(0, [Byte]))))

is now somewhat working. The problem with it now is it's only getting the first 3 characters of the expected string. for instance the string white.tga is only coming back whi

Upvotes: 0

Views: 1538

Answers (1)

Phillip Proctor
Phillip Proctor

Reputation: 59

This was my final solution. It DOES indeed work. There is an overload for the Array.IndexOf that lets you specify a starting point for the search

MyClass.Textures(x) = System.Text.Encoding.ASCII.GetString(DataBlock, BlockData + y, Array.IndexOf(DataBlock, CType(0, [Byte]), BlockData + y))

Here it is all wrapped up in a nice little function:

Public Function GetNullTermStr(ByVal ByteArray As Byte(), ByVal Start As Integer) As String
        ' Gets a null (&H0) terminated string from a ByteArray starting at location Start
        Try
            Return System.Text.Encoding.ASCII.GetString(ByteArray, Start, Array.IndexOf(ByteArray, CType(0, [Byte]), Start))
        Catch ex As Exception
            MsgBox(ex.Message)
            Return Nothing
        End Try
    End Function

Upvotes: 1

Related Questions