C#使用正則表達式來驗證是否是16進制字符串


        /// <summary>
        /// 判斷是否為16進制字符串
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static bool IsHexString(string hexString)
        {
            //十六進制發送時,發送框數據進行十六進制數據正則校驗
            if (Regex.IsMatch(hexString, "^[0-9A-Fa-f]+$"))
            {
                //校驗成功
                return true;
            }
            //校驗失敗
            return false;
        }

 發送數據

 /// <summary>
        /// 發送數據
        /// </summary>
        private void SendData()
        {
            if (this.chk_HexSend.Checked)
            {
                //去掉空格  01 04   50
                string temp = this.rtb_Send.Text.Replace(" ", "");

                try
                {
                    byte[] b = HexHelper.HexStringToBytes(temp);

                    serialPort.Write(b, 0, b.Length);

                    TotalSendNum += b.Length;
                }
                catch (Exception ex)
                {
                    this.tssl_Status.Text = "發送失敗:" + ex.Message;
                    IsOpen = false;
                }
            }

            else
            {
                try
                {
                    byte[] b = encoding.GetBytes(this.rtb_Send.Text);

                    serialPort.Write(b, 0, b.Length);

                    TotalSendNum += b.Length;
                }
                catch (Exception ex)
                {
                    this.tssl_Status.Text = "發送失敗:" + ex.Message;
                    IsOpen = false;
                }
            }
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM