Hex String From Byte Array - CSharp System

CSharp examples for System:Byte

Description

Hex String From Byte Array

Demo Code


using System.Windows.Forms;
using System.IO;//from  ww  w . j  a v a2s . c  o m
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;

public class Main{
        public static string HexStringFromByteArray(byte[] baArrayToDump, bool bSpaceDelimit)
        {
            string sRet = string.Empty;

    
            StringBuilder oSB = new StringBuilder();

            string sHex = string.Empty;
            int iValue = 0;
            int iItem = 0;

            string sText = string.Empty;

 
            foreach (byte oByte in baArrayToDump)
            {
                iValue = oByte;
                iItem += 1;

                // Clean junk charaters...
                if (char.IsControl((char)iValue))
                {
                    sText += " ";

                }
                else
                {
                    sText += string.Format("{0}", (char)iValue).PadLeft(1, ' ');
                }


                if (bSpaceDelimit)
                    oSB.Append(" ");

                sHex = string.Format("{00:X}", iValue).PadLeft(2, '0');
                oSB.Append(sHex);
                 
            }
            sRet = oSB.ToString();
            return sRet;

        }
}

Related Tutorials