Convert from a compressed UTF8 string to C# string. - CSharp System

CSharp examples for System:String Unicode

Description

Convert from a compressed UTF8 string to C# string.

Demo Code

// Copyright (c) Microsoft. All rights reserved.
using System.Text;
using System.Collections.Generic;
using System;//w w  w  .ja  va  2s . c  om

public class Main{
        /// <summary>
        /// Convert from a compressed UTF8 string to C# string.
        /// </summary>
        /// <param name="compressedUtf8String">A compressed UTF8 string.</param>
        /// <returns>The string.</returns>
        /// <exception cref="ArgumentNullException">
        /// Thrown when compressedUtf8String is null.</exception>
        public static string FromCompressedUtf8String(byte[] compressedUtf8String)
        {
            if (compressedUtf8String == null)
            {
                throw new ArgumentNullException("compressedUtf8String");
            }

            string domainName = string.Empty;
            int offset = 0;
            while (offset < compressedUtf8String.Length)
            {
                int labelLength = compressedUtf8String[offset];
                offset += sizeof(byte);
                if (labelLength == 0)
                {
                    break;
                }

                if (domainName.Length > 0)
                {
                    domainName += '.';
                }
                domainName += Encoding.UTF8.GetString(compressedUtf8String, offset, labelLength);
                offset += labelLength;
            }

            return domainName;
        }
}

Related Tutorials