zoukankan      html  css  js  c++  java
  • C# 十进制与十六进制互转

    1.从十六进制转换为十进制

        /// <summary>
            /// 十六进制转换到十进制
            /// </summary>
            /// <param name="hex"></param>
            /// <returns></returns>
            public static string Hex2Ten(string hex)
            {
                int ten = 0;
                for (int i = 0, j = hex.Length - 1; i < hex.Length; i++)
                {
                    ten += HexChar2Value(hex.Substring(i, 1)) * ((int)Math.Pow(16, j));
                    j--;
                }
                return ten.ToString();
            }
    
            public static int HexChar2Value(string hexChar)
            {
                switch (hexChar)
                {
                    case "0":
                    case "1":
                    case "2":
                    case "3":
                    case "4":
                    case "5":
                    case "6":
                    case "7":
                    case "8":
                    case "9":
                        return Convert.ToInt32(hexChar);
                    case "a":
                    case "A":
                        return 10;
                    case "b":
                    case "B":
                        return 11;
                    case "c":
                    case "C":
                        return 12;
                    case "d":
                    case "D":
                        return 13;
                    case "e":
                    case "E":
                        return 14;
                    case "f":
                    case "F":
                        return 15;
                    default:
                        return 0;
                }
            }
    this.txtStartShi.Text = Hex2Ten(this.txtStartSN.Text.Trim().Substring(4, 8));
    this.txtEndShi.Text = Hex2Ten(this.txtEndSN.Text.Trim().Substring(4, 8));

    2.从十进制转换为十六进制

            /// <summary>
            /// 从十进制转换到十六进制
            /// </summary>
            /// <param name="ten"></param>
            /// <returns></returns>
            public static string Ten2Hex(string ten)
            {
                ulong tenValue = Convert.ToUInt64(ten);
                ulong divValue, resValue;
                string hex = "";
                do
                {
                    //divValue = (ulong)Math.Floor(tenValue / 16);
    
                    divValue = (ulong)Math.Floor((decimal)(tenValue / 16));
    
                    resValue = tenValue % 16;
                    hex = tenValue2Char(resValue) + hex;
                    tenValue = divValue;
                }
                while (tenValue >= 16);
                if (tenValue != 0)
                    hex = tenValue2Char(tenValue) + hex;
                return hex;
            }
    
            public static string tenValue2Char(ulong ten)
            {
                switch (ten)
                {
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                    case 8:
                    case 9:
                        return ten.ToString();
                    case 10:
                        return "A";
                    case 11:
                        return "B";
                    case 12:
                        return "C";
                    case 13:
                        return "D";
                    case 14:
                        return "E";
                    case 15:
                        return "F";
                    default:
                        return "";
                }
            }
     int StartSN = Convert.ToInt32(this.txtStartShi.Text.Trim());
    int EndSN = Convert.ToInt32(this.txtEndShi.Text.Trim());
    
      for (int i = 0; i < EndSN - StartSN + 1; i++)
      {
        listBox1.Items.Add("0014" + Ten2Hex((Convert.ToDouble(this.txtStartShi.Text) + i).ToString()));
      }

        /// <summary>
            /// 10进制转34进制
            /// </summary>
            /// <param name="parameter"></param>
            /// <returns></returns>
            public static string Ten2ThirtyFour(int parameter)
            {
                string[] radix = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", 
                                     "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", 
                                     "U", "V", "W", "X", "Y", "Z" };
                string result = "";
                int len = 0;
                int remainder = 0;
                len = parameter / 34;
                remainder = parameter % 34;
                result = radix[remainder];
                while (len > 0)
                {
                    remainder = len % 34;
                    len = len / 34;                
                    result = radix[remainder] + result;
                }                        
                return result;
            }
  • 相关阅读:
    IntelliJ IDEA 2019.3 安装+永久破解[Windows]
    word2016中如何实现类似目录样式的虚线对齐效果,可用于制作checklist
    测绘地图资源不够用?教你个万能图源制作方法
    真实评测:全球卫星地图哪个最清晰?
    【地图导航】3D地图软件是如何做路径规划的?为什么准确率这么高
    国产良心极简版地图软件,地图下载很丝滑,界面简洁无广告
    推荐几款实用性强的外业勘察地图软件
    重要通知!奥维地图被紧急下架,还有什么地图软件能用呢?
    单片机平台环境
    关于最近登陆需要验证手机
  • 原文地址:https://www.cnblogs.com/allen0118/p/3892028.html
Copyright © 2011-2022 走看看