1. 程式人生 > >C# 獲取字串轉Hex格式和BCD格式

C# 獲取字串轉Hex格式和BCD格式

一、字串轉BCD格式

1、設計要求

上位機獲取輸入的年月日字串,轉換成BCD格式後傳遞到下位機。

輸入一串序列號,最小1位,年最大4位;月和日最小一位,最大兩位。

 

2、設施步驟

(1)獲取字串,判斷字串的長度是否符合要求。

(2)判讀字串是否是十進位制數。

(3)字串不滿足要求位數,左邊用0補齊。

(4)字串轉成BCD

3、程式程式碼


            //獲取韌體日期

            //年
            numbeString = CRCYear.Text;
            if (JudgeData(numbeString, 1, 4, 0))//判斷輸入的字串最短一位,最長4位  數值0-9
            {
                verSetSerNum[16] = (string_to_BCD(numbeString)[1]);//轉換成BCD碼
                verSetSerNum[17] = (string_to_BCD(numbeString)[0]);
            }
            else
            {
                MessageBox.Show("年的格式不正確", "錯誤");
            }

            //月
            numbeString = CRCMonth.Text;
            if (JudgeData(numbeString, 1, 2, 0))//判斷輸入的字串最短一位,最長2位   數值0-9
            {
                int[] num = new int[2];
                //字串分割  2個字串平均分成2個字串
                string[] subString = new string[2];
                for (int i = 0; i < 2; i++)
                {
                    subString[i] = numbeString.Substring(i, 1);//字串分割
                    num[i] = int.Parse(subString[i], System.Globalization.NumberStyles.HexNumber);//字串轉int型別
                }
                //先發送低位資料再發送高位資料

                verSetSerNum[18] = (byte)num[1];
                verSetSerNum[19] = (byte)num[0];
            }
            else
            {
                MessageBox.Show("月的格式不正確", "錯誤");
            }

            //日
            numbeString = CRCDay.Text;
            if (JudgeData(numbeString, 1, 2, 0))//判斷輸入的字串最短一位,最長2位   數值0-9
            {
                int[] num = new int[2];
                //字串分割  2個字串平均分成2個字串
                string[] subString = new string[2];
                for (int i = 0; i < 2; i++)
                {
                    subString[i] = numbeString.Substring(i, 1);//字串分割
                    num[i] = int.Parse(subString[i], System.Globalization.NumberStyles.HexNumber);//字串轉int型別
                }
                //先發送低位資料再發送高位資料

                verSetSerNum[20] = (byte)num[1];
                verSetSerNum[21] = (byte)num[0];
            }
            else
            {
                MessageBox.Show("日的格式不正確", "錯誤");
            }
//日期格式判斷

public bool JudgeData(string strMessage, int iMinLong, int iMaxLong, int f)
        {
            bool bResult = false;
            if (f == 1)
                number_string += strMessage;
            //開頭匹配一個字母或數字+匹配兩個十進位制數字+匹配一個字母或數字+匹配兩個相同格式的的(-加數字)+已字母或數字結尾  

            string pattern = @"^[0-9]*$"; //輸入的字串為十進位制數
            if (strMessage.Length >= iMinLong && strMessage.Length <= iMaxLong)//判斷字串長度是否在要求範圍內
            {
                if (System.Text.RegularExpressions.Regex.IsMatch(strMessage, pattern))//匹配所有字元都在字母和數字之間
                {
                    bResult = true;
                }
                else
                {
                    bResult = false;
                }
                //字串的位數不滿iMaxLong位時,左側用0補齊
                if (strMessage.Length < iMaxLong)
                {
                    numbeString = numbeString.PadLeft(iMaxLong, '0');//字串固定為iMaxLong為,不足左側補0
                }
            }

            return bResult;
        }  

  //字串轉BCD碼方法
        private static Byte[] string_to_BCD(string strTemp)
        {
            try
            {
                if (Convert.ToBoolean(strTemp.Length & 1))//若字串的長度為奇數,則變為偶數 
                {
                    strTemp = "0" + strTemp;//數位為奇數時前面補0  
                }
                Byte[] aryTemp = new Byte[strTemp.Length / 2];
                for (int i = 0; i < (strTemp.Length / 2); i++)
                {
                    aryTemp[i] = (Byte)(((strTemp[i * 2] - '0') << 4) | (strTemp[i * 2 + 1] - '0'));//兩個位元組的陣列成一個位元組的BCD碼0-9
                   // aryTemp[i] = (Byte)(((strTemp[i * 2])<< 4) | (strTemp[i * 2 + 1]));
                }
                return aryTemp;//高位在前  
            }
            catch
            {
                MessageBox.Show("set failed!");
                return null;
            } 
        }

二、字串轉Hex格式

1、設計要求

獲取文字框中輸入的16進位制字串,將其通過串列埠傳送到下位機。

輸入一串序列號,序列號最小1位,最大12位。

如輸入20181009,則 hardwareVerSn[0]=0x1009,hardwareVerSn[1]=0x2018,hardwareVerSn[2]=0x0000

2、設施步驟

1)字串長度判斷:

(1)獲取字串,判斷字串的長度是否符合要求。

(2)判讀字串是否是16進位制數。

(3)字串不滿足12位,左邊用字元0補齊。

2)字串轉成hex

(1)字串分割(12個字串平均分割成6個字串)

(2)分割後的每個字串轉int型別

(3)int型別轉byte型別

3、程式

(1)獲取輸入的字串

 numbeString = hardwareVerSn1.Text;

(2)判斷字串格式並轉換

  if (JudgeVisionNum(numbeString, 1, 12, 0))//格式判斷   最大輸入12位數 每位數為0-f  不足12位前面補零
            {
                int[] num = new int[6];
                //字串分割  12個字串平均分成6個字串
                string[] subString = new string[6];
                for (int i = 0; i < 6; i++)
                {
                    subString[i] = numbeString.Substring(i * 2, 2);//字串分割
                    num[i] = int.Parse(subString[i], System.Globalization.NumberStyles.HexNumber);//字串轉int型別
                }
                //先發送低位資料再發送高位資料
                verSetSerNum[2] = (byte)num[5];//int轉byte型別
                verSetSerNum[3] = (byte)num[4];
                verSetSerNum[4] = (byte)num[3];
                verSetSerNum[5] = (byte)num[2];
                verSetSerNum[6] = (byte)num[1];
                verSetSerNum[7] = (byte)num[0];
                 
            }


        //判斷版本號是否符合要求
        public bool JudgeVisionNum(string strMessage, int iMinLong, int iMaxLong, int f)
        {
            bool bResult = false;
            if (f == 1)
                number_string += strMessage;
            //開頭匹配一個字母或數字+匹配兩個十進位制數字+匹配一個字母或數字+匹配兩個相同格式的的(-加數字)+已字母或數字結尾  

          //  string pattern = @"^[0-9]*$"; 
            string pattern = @"^[0-9A-Fa-f]+$";//匹配十六進位制
            if (strMessage.Length >= iMinLong && strMessage.Length <= iMaxLong)//判斷字串長度是否在要求範圍內
            {
                if (System.Text.RegularExpressions.Regex.IsMatch(strMessage, pattern))//匹配所有字元都在字母和數字之間
                {
                    bResult = true;
                }
                else
                {
                    bResult = false;
                }
                //字串的位數不滿iMaxLong位時,左側用0補齊
                if(strMessage.Length < iMaxLong)
                {
                    numbeString = numbeString.PadLeft(iMaxLong, '0');//字串固定為iMaxLong為,不足左側補0
                }
            }

            return bResult;
        }