Converts a byte array to a hex-encoded string. - CSharp System

CSharp examples for System:Hex

Description

Converts a byte array to a hex-encoded string.

Demo Code

// Copyright (c) .NET Foundation. All rights reserved.
using Microsoft.AspNet.WebHooks.Properties;
using System.Globalization;
using System;//w  w w .  j av a  2s.  c  o  m

public class Main{
        /// <summary>
        /// Converts a <see cref="T:byte[]"/> to a hex-encoded string.
        /// </summary>
        public static string ToHex(byte[] data)
        {
            if (data == null)
            {
                return string.Empty;
            }

            char[] content = new char[data.Length * 2];
            int output = 0;
            byte d;
            for (int input = 0; input < data.Length; input++)
            {
                d = data[input];
                content[output++] = HexLookup[d / 0x10];
                content[output++] = HexLookup[d % 0x10];
            }
            return new string(content);
        }
}

Related Tutorials