예제 #1
0
        /// <summary>
        /// 生成随机MAC地址
        /// </summary>
        /// <returns></returns>
        public static MAC GetRandomMac()
        {
            var    result = new MAC();
            Random random = new Random();

            random.NextBytes(result.mac);
            return(result);
        }
예제 #2
0
        /// <summary>
        /// 尝试将字符串格式的解析为 MAC 格式
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="errorMessage">如果解析出错,会返回错误信息</param>
        /// <param name="result">如果解析出错,result=null</param>
        /// <returns>是否解析成功</returns>
        public static bool TryParse(string str, out string errorMessage, out MAC result)
        {
            result = null;
            bool canParse = CanParse(str, out errorMessage);

            if (canParse)
            {
                result = new MAC();
                var hexs = str.Split(' ');
                for (int i = 0; i < 6; i++)
                {
                    result.mac[i] = Convert.ToByte(hexs[i], 16);
                }
            }

            return(canParse);
        }