伊莉討論區

標題: C# 正規表示法16進位數字轉十進位 [打印本頁]

作者: w100386435    時間: 2018-6-8 09:16 AM     標題: C# 正規表示法16進位數字轉十進位

提示: 作者被禁止或刪除 內容自動屏蔽
作者: sggleeee    時間: 2018-6-8 08:43 PM

關於16進位字串轉10進位數字,大大不妨考慮用Convert.ToInt64方法處理

Convert.ToInt64用法:
Convert.ToInt64(string value, IFormatProvider provider)

如果要處理輸入的字元限定為0-9 A-F, 可以在TextBox的Keypress用常規表示法處理

底下提供一個簡單做法供大大參考:
  1. namespace WindowsFormsApplication1
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.         public Form1()
  6.         {
  7.             InitializeComponent();
  8.         }

  9.         private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  10.         {
  11.             string pattern = "[0-9A-Fa-f\b]";
  12.             Regex rgx = new Regex(pattern);
  13.             if (!rgx.Match(e.KeyChar.ToString()).Success)
  14.                 e.Handled = true;
  15.         }

  16.         private void button1_Click(object sender, EventArgs e)
  17.         {
  18.             textBox2.Text = Convert.ToInt64(textBox1.Text, 16).ToString();
  19.         }

  20.     }
  21. }
複製代碼
提醒:
1. 要使用常規表示法請記得
  1. using System.Text.RegularExpressions;
複製代碼
2. Hex String 若超過 Int64, 記得處理Overflow
作者: CPX-900TR    時間: 2023-2-25 01:59 AM

本帖最後由 CPX-900TR 於 2023-2-25 02:09 AM 編輯

懇求高手解疑惑!10位數字轉6位數算法?
http://www.eyny.com/thread-13647332-1-1.html





歡迎光臨 伊莉討論區 (https://www71.eyny.com/) Powered by Discuz!