Trying to figure out memorystreams and byte arrays

cpopham

Well-known member
Joined
Feb 18, 2004
Messages
273
Okay,

I am attempting to learn more about memorystreams and byte arrays as well because I think my hashing and crytography problems are arising from this area. I am reading a text file into an array, extracting a portion of the array to a memorystream, then writing that back out to disk. Just to see what is going on. Okay, I have everything working fine except the extracting of a portion of the array to the memorystream.

Since it is string characters, I am doing something simple, I only want the last 9 characters of the original text file.
I thought that I could use length - 9 to get those, or maybe since it is 0 based length - 10. However, that is not the case, to get the last 9 characters, I have to say something like length-11. Why 11? This is an odd number to me. Am I missing something here?

Here is my code:
Code:
        Dim encoder As New UTF8Encoding /Create UTF8 encoding (has more applications than ASCII)
        Dim fsIn As New FileStream(strOutFileArray, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) /Setup input filestream
        Dim rdIn As New StreamReader(fsIn) /Setup streamreader
        Dim myfile As Byte() /ByteArray to hold input file
        myfile = encoder.GetBytes(rdIn.ReadToEnd) /Read input file in
        Dim msstream As New MemoryStream(11) /Setup memorystream to hold data with length of data
        msstream.Write(myfile, myfile.Length - 11, 11) /Convert byte array of data to memorystream
        Dim after As Byte() /Byte array to hole data from memorystream
        after = msstream.ToArray /Convert memorystream to byte array

        Dim intLength As Integer = msstream.Length / 8
        Dim strLength As String = intLength
        strLength = strLength.PadLeft(9, "0")
        Dim strJunk As String = "WE DO NOT WANT THIS LINE"

        /Setup output Filestream
        Dim fsOut As New FileStream(strOutFileArray2, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
        Dim wr As New StreamWriter(fsOut) /Setup streamwriter to output data
        wr.Write(encoder.GetString(after)) /Convert byte array to string and write it to disk
        wr.WriteLine(strJunk)
        wr.WriteLine(strLength)
        wr.Flush() /Flush the streamwriter to make sure all data is written to disk
Could someone please explain this to me?

Thanks, Chester
 
Last edited by a moderator:
Back
Top