hello world!!!!!

写下自己的一些心得,写下自己问题的方式,写下程序之路的艰辛,希望能够有朝一日成为大牛。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

Posted on 2010-10-22 10:28  陈力  阅读(241)  评论(0编辑  收藏  举报

/*
     *
     *        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