1、先将取出来的16进制卡号颠倒如F3 0C 4F 06转成06 4F 0C F3 (注意物理卡号是第0扇区的前4个字节)
2、将转换后的16进制卡号转成10进制,不足10位左补0
代码实现:
//调用: GetM1CardNo("F30C4F06");
1 private string GetM1CardNo(string cardNo16Code) 2 { 3 string cardNo = ""; 4 //十六进制反转 5 int i = 0; 6 string s = ""; 7 string[] arr = new string[cardNo16Code.Length / 2]; 8 foreach (var item in cardNo16Code) 9 { 10 s = s + item; 11 i = i + 1; 12 if (i % 2 == 0) 13 { 14 arr[i/2 -1] = s; 15 s = ""; 16 } 17 } 18 Array.Reverse(arr); 19 20 //转10进制,不足10左补0 21 cardNo = GetHexadecimalValue(string.Join("", arr)).ToString().PadLeft(10, '0'); 22 23 return cardNo; 24 }
1 /// <summary> 2 /// 十六进制换算为十进制 3 /// </summary> 4 /// <param name="strColorValue"></param> 5 /// <returns></returns> 6 public static int GetHexadecimalValue(String strColorValue) 7 { 8 char[] nums = strColorValue.ToCharArray(); 9 int total = 0; 10 try 11 { 12 for (int i = 0; i < nums.Length; i++) 13 { 14 String strNum = nums[i].ToString().ToUpper(); 15 switch (strNum) 16 { 17 case "A": 18 strNum = "10"; 19 break; 20 case "B": 21 strNum = "11"; 22 break; 23 case "C": 24 strNum = "12"; 25 break; 26 case "D": 27 strNum = "13"; 28 break; 29 case "E": 30 strNum = "14"; 31 break; 32 case "F": 33 strNum = "15"; 34 break; 35 default: 36 break; 37 } 38 double power = Math.Pow(16, Convert.ToDouble(nums.Length - i - 1)); 39 total += Convert.ToInt32(strNum) * Convert.ToInt32(power); 40 } 41 42 } 43 catch (System.Exception ex) 44 { 45 String strErorr = ex.ToString(); 46 return 0; 47 } 48 49 50 return total; 51 }
标签:case,10,string,16,C#,break,M1,strNum From: https://www.cnblogs.com/handsomeziff/p/16850243.html