十六进制与十进制的相互转换 [ 原创create by lee ]

/*
     *
     *        creater:create by lee
     *        fuction : use to number to hex
     *        date :2010-10-21
     *
     *
     */
    public class HexUtility
    {
        /// <summary>
        /// int类型转换成十六进制
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public  string ToHexSystem(int number)
        {
            byte[] result = BitConverter.GetBytes(number);
            string result1 = BitConverter.ToString(result);
            return result1;
        }
        /// <summary>
        /// int类型转换成十六进制
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public  string Tohex(int number)
        {
            int _aid = number;
            var hexChars = "0123456789ABCDEF";
            var _xaid = "";
            do
            {
                var i = _aid % 16;
                _aid = (_aid - i) / 16;
                _xaid = hexChars[i] + _xaid;
            } while (_aid > 0);
            while (_xaid.Length < 8)
            {
                _xaid = "0" + _xaid;
            }
            return _xaid;
        }

        /// <summary>
        /// 十六进制转换成int类型
        /// </summary>
        /// <param name="hex">0AFC9C1B</param>
        public int HexToNumber(string hex)
        {

           return  Convert.ToInt32(hex, 16);
        }
    }                    如果转载请注明来源 create by lee

原文地址:https://www.cnblogs.com/chenli0513/p/1858064.html