Outputs a space separated array of hexadecimal characters - CSharp System

CSharp examples for System:Array Element

Description

Outputs a space separated array of hexadecimal characters

Demo Code

//  Licensed under the Apache License, Version 2.0 (the "License");
using System.Collections;
using System.Text;
using System;//from www  . j av a2  s.  co  m

public class Main{
    /// <summary>
      /// Outputs a space separated array of hexadecimal characters
      /// </summary>
      /// <param name="offset">Start index</param>
      /// <param name="length">Number of bytes to process</param>
      /// <returns>Space separated array of hexadecimal characters</returns>
      public static string ToHex(this byte[] array, int offset, int length)
      {
         if (array == null || array.Length == 0 || array.Length < (offset + length))
            return String.Empty;

         byte dataByte;
         char[] chars = new char[length * 3];
         int end = offset + length;
         int j = 0;
         for (int i = offset; i < end; i++)
         {
            dataByte = array[i];
            chars[j++] = hexValues[(dataByte >> 4) & 0x0F];
            chars[j++] = hexValues[dataByte & 0x0F];
            chars[j++] = ' ';
         }

         return new string(chars, 0, chars.Length);
      }
        /// <summary>
      /// Outputs a space separated array of hexadecimal characters
      /// </summary>
      /// <param name="length">Number of bytes to process</param>
      /// <returns>Space separated array of hexadecimal characters</returns>
      public static string ToHex(this byte[] array, int length)
      {
         if (array == null || array.Length == 0 || length == 0)
            return String.Empty;

         return ToHex(array, 0, length);
      }

      /// <summary>
      /// Outputs a space separated array of hexadecimal characters
      /// </summary>
      /// <returns>Space separated array of hexadecimal characters</returns>
      public static string ToHex(this byte[] array)
      {
         if (array == null || array.Length == 0)
            return String.Empty;

         return ToHex(array, 0, array.Length);
      }
}

Related Tutorials