SHARE
TWEET

Untitled

a guest Jul 17th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Globalization;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace gist.yifeitao.com
  7. {
  8.     [Serializable]
  9.     public class Mac
  10.     {
  11.         private readonly byte[] _address = new byte[6];
  12.         /// <summary>
  13.         /// use byte array to construct
  14.         /// </summary>
  15.         /// <param name="address">a byte array(length==6)</param>
  16.         public Mac(byte[] address)
  17.         {
  18.             if (address.Length != 6)
  19.             {
  20.                 throw new ArgumentException("address.Length != 6");
  21.             }
  22.             Array.Copy(address, _address, 6);
  23.         }
  24.         /// <summary>
  25.         /// get a copy of address in bytes
  26.         /// </summary>
  27.         /// <returns></returns>
  28.         public byte[] GetAddressBytesCopy()
  29.         {
  30.             byte[] copy = new byte[6];
  31.             Array.Copy(_address,copy,6);
  32.             return copy;
  33.  
  34.         }
  35.         /// <summary>
  36.         /// Parse a string to mac
  37.         /// </summary>
  38.         /// <param name="s">a string looks like "XX-XX-XX-XX-XX-XX"</param>
  39.         /// <returns>Mac address</returns>
  40.         public static Mac Parse(string s)
  41.         {
  42.             Regex regex = new Regex("[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]");
  43.             if (!regex.IsMatch(s)) throw new ArgumentException("the string can't mactch XX-XX-XX-XX-XX-XX");
  44.             var address = ParesAddressBytes(s);
  45.             return new Mac(address);
  46.         }
  47.  
  48.         private static byte[] ParesAddressBytes(string s)
  49.         {
  50.             byte[] address = new byte[6];
  51.             string[] addrStrings = s.Split('-');
  52.             for (int i = 0; i < 6; i++)
  53.             {
  54.                 address[i] = byte.Parse(addrStrings[i], NumberStyles.HexNumber);
  55.             }
  56.             return address;
  57.         }
  58.  
  59.         public bool TryParse(string s, out Mac mac)
  60.         {
  61.             Regex regex = new Regex("[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]");
  62.             mac = null;
  63.             if (!regex.IsMatch(s)) return false;
  64.             var address = ParesAddressBytes(s);
  65.             mac = new Mac(address);
  66.             return true;
  67.         }
  68.  
  69.         public override string ToString()
  70.         {
  71.             StringBuilder sb = new StringBuilder();
  72.             sb.Append(_address[0].ToString("X2"));
  73.             sb.Append("-");
  74.             sb.Append(_address[1].ToString("X2"));
  75.             sb.Append("-");
  76.             sb.Append(_address[2].ToString("X2"));
  77.             sb.Append("-");
  78.             sb.Append(_address[3].ToString("X2"));
  79.             sb.Append("-");
  80.             sb.Append(_address[4].ToString("X2"));
  81.             sb.Append("-");
  82.             sb.Append(_address[5].ToString("X2"));
  83.             return sb.ToString();
  84.         }
  85.  
  86.         public override bool Equals(object obj)
  87.         {
  88.             if (!(obj is Mac))
  89.             {
  90.                 return false;
  91.             }
  92.             byte[] bytes1 = GetAddressBytesCopy();
  93.             byte[] bytes2 = ((Mac) obj).GetAddressBytesCopy();
  94.             for (int i = 0; i < 6; i++)
  95.             {
  96.                 if (bytes1[i] != bytes2[i])
  97.                     return false;
  98.             }
  99.             return true;
  100.         }
  101.  
  102.         public override int GetHashCode()
  103.         {
  104.             return ((_address[0] << 40) & (_address[1] << 32) & (_address[2] << 24)
  105.                     & (_address[3] << 16) & (_address[4] << 8) & _address[5])
  106.                 .GetHashCode();
  107.         }
  108.     }
  109. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top