Outputs a formatted (##-##-##-##-##-##) MAC Address string - CSharp System.Net

CSharp examples for System.Net:Mac Address

Description

Outputs a formatted (##-##-##-##-##-##) MAC Address string

Demo Code

//  Licensed under the Apache License, Version 2.0 (the "License");
using System.Collections;
using System.Text;
using System;/*  w  w  w  .ja  v a2  s . com*/

public class Main{
      /// <summary>
      /// Outputs a formatted (##-##-##-##-##-##) MAC Address string
      /// </summary>
      /// <param name="offset">Start index</param>
      /// <returns>Formatted MAC address string</returns>
      public static string ToMACAddress(this byte[] array, int offset)
      {
         if (array == null || array.Length == 0 || offset < 0 || offset >= array.Length)
            return String.Empty;

         string[] macAddr = new string[6];

         macAddr[0] = array[offset + 0].ToHex();
         macAddr[1] = array[offset + 1].ToHex();
         macAddr[2] = array[offset + 2].ToHex();
         macAddr[3] = array[offset + 3].ToHex();
         macAddr[4] = array[offset + 4].ToHex();
         macAddr[5] = array[offset + 5].ToHex();

         return String.Concat(macAddr[0], "-", macAddr[1], "-", macAddr[2], "-", macAddr[3], "-", macAddr[4], "-", macAddr[5]);
      }
        /// <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