0

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

7
  • what happens if there is no &H0 in the array after y? or it is before it. Also the third parameter is the length not to Commented Dec 16, 2015 at 7:15
  • there is always a zero. Sometimes after the actual block there are several of them. I realize the third parameter is the length however what it's trying to do is specify that length as the occurrence of &H0. I will admit I don't fully understand the function though so I will go test some things and report back. Commented Dec 16, 2015 at 7:23
  • the function is simple it reads data from the first parameter, the second parameter controls from where the read starts (0 based index), and the third parameter specifies how many bytes to be read. Commented Dec 16, 2015 at 7:26
  • Update: For starters I had some stupid errors. For starters 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" Commented Dec 16, 2015 at 7:36
  • I am thinking that Array.IndexOf(DataBlock, CType(0, [Byte])) is actually starting at the beginning of the file instead of from where the array started looking. I am almost certain of that. so now the question is how do I get array.indexof to start its search at Datablock(offset)? Commented Dec 16, 2015 at 7:39

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.