Reads a 4 byte length prefixed ansi string from the given BinaryReader - CSharp System

CSharp examples for System:Byte

Description

Reads a 4 byte length prefixed ansi string from the given BinaryReader

Demo Code

/*//from ww w.  j a  va 2s.c  o  m
   Copyright 2013 - 2016 Kees van Spelde

   Licensed under The Code Project Open License (CPOL) 1.02;
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.codeproject.com/info/cpol10.aspx

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System.Text;
using System.IO;

public class Main{
        #endregion

        #region Read4ByteLengthPrefixedUnicodeString
        /// <summary>
        ///     Reads a 4 byte length prefixed ansi string from the given <paramref name="binaryReader" />
        /// </summary>
        /// <param name="binaryReader"></param>
        /// <returns></returns>
        internal static string Read4ByteLengthPrefixedUnicodeString(BinaryReader binaryReader)
        {
            var length = (int)binaryReader.ReadUInt32() * 2;
            var bytes = binaryReader.ReadBytes(length);
            var str = Encoding.Unicode.GetString(bytes);
            return str.TrimEnd('\0');
        }
}

Related Tutorials