客戶給了一個十六進制的條碼范圍,只有起始和結束,中間的條碼都不知道,現在需要將這些十六進制的條碼全部打印成條碼,然后貼在成品上面,如果是普通的阿拉伯數字那么直接循環+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));
條碼取到之后就隨便怎么做了。