C#操作十六进制数据以及十进制与十六进制互相转换

客户给了一个十六进制的条码范围,只有起始和结束,中间的条码都不知道,现在需要将这些十六进制的条码全部打印成条码,然后贴在成品上面,如果是普通的阿拉伯数字那么直接循环+1,使用 流水号就行了,但是对十六进制的条码相对来说麻烦了一点点,实现方式如下:

1.首先将客户给的那个十六进制的起始和结束条码转换为十进制,转换成十进制之后就可以计算了。
方法:

/// <summary>
        /// 从十进制转换到十六进制
        /// </summary>
        /// <param name="ten"></param>
        /// <returns></returns>
        public static string ConvertNumToHex(string ten)
        {
            ulong Numb = Convert.ToUInt64(ten);
            ulong divValue, resValue;
            string hex = "";
            do
            { 
                divValue = (ulong)Math.Floor((decimal)(Numb / 16));

                resValue = tenValue % 16;
                hex = GetNumb(resValue) + hex;
                Numb = divValue;
            }
            while (Numb >= 16);
            if (Numb != 0)
                hex = GetNumb(Numb) + hex;
            return hex;
        }

        public static string GetNumb(ulong Numb)
        {
            switch (Numb)
            {
                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 "";
            }
        }

调用这个方法将结果取出来:

 private void btnGetNumb_Click(object sender, EventArgs e)
        {
            this.txtStartSN.Text = Hex2Ten(this.txtStartSNHex.Text.Trim().Substring(4, 8));
            this.txtEndSN.Text = Hex2Ten(this.txtEndSNHex.Text.Trim().Substring(4, 8));

        }

2.根据得到的十进制条码范围生成条码

listBox1.Items.Add(ConvertNumToHex((Convert.ToDouble(this.txtStartSN.Text) + i).ToString()).Substring(4, 4));


条码取到之后就随便怎么做了。

【推广】 免费学中医,健康全家人
原文地址:https://www.cnblogs.com/allen0118/p/2803331.html