ME3Explorer.TalkFile.GetString C# (CSharp) Method

GetString() private method

Starts reading 'Bits' array at position 'bitOffset'. Read data is used on a Huffman Tree to decode read bits into real strings. 'bitOffset' variable is updated with last read bit PLUS ONE (first unread bit).
Global variables used: List(of HuffmanNodes) CharacterTree BitArray Bits
private GetString ( int &bitOffset ) : string
bitOffset int
return string
        private string GetString(ref int bitOffset)
        {
            HuffmanNode root = CharacterTree[0];
            HuffmanNode curNode = root;

            string curString = "";
            int i;
            for (i = bitOffset; i < Bits.Length; i++)
            {
                /* reading bits' sequence and decoding it to Strings while traversing Huffman Tree */
                int nextNodeID;
                if (Bits[i])
                    nextNodeID = curNode.RightNodeID;
                else
                    nextNodeID = curNode.LeftNodeID;

                /* it's an internal node - keep looking for a leaf */
                if (nextNodeID >= 0)
                    curNode = CharacterTree[nextNodeID];
                else
                /* it's a leaf! */
                {
                    char c = BitConverter.ToChar(BitConverter.GetBytes(0xffff - nextNodeID), 0);
                    if (c != '\0')
                    {
                        /* it's not NULL */
                        curString += c;
                        curNode = root;
                    }
                    else
                    {
                        /* it's a NULL terminating processed string, we're done */
                        bitOffset = i + 1;
                        return curString;
                    }
                }
            }
            bitOffset = i + 1;
            return null;
        }