頁:
[1]
C# 正規表示法16進位數字轉十進位
本帖最後由 w100386435 於 2018-6-8 09:28 AM 編輯簡單的範例:會找出字串中所有16進位數字 並且替換成十進位
public string conver_hex(string text) {
Regex r = new Regex("0+", RegexOptions.IgnoreCase);
Match m = r.Match(text);
while (m.Success) {
string hex_str= m.Value; //轉換 long value= long.Parse(hex_str.Substring(2), System.Globalization.NumberStyles.HexNumber);
text=text.Replace(hex_str, value.ToString());
m = m.NextMatch();
}
return text;
}
好讀版請自己在空白處補.
null-adventure-diarytwo blogspot com/2018/06/c-16.html
...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div><div></div> 關於16進位字串轉10進位數字,大大不妨考慮用Convert.ToInt64方法處理
Convert.ToInt64用法:
Convert.ToInt64(string value, IFormatProvider provider)
如果要處理輸入的字元限定為0-9 A-F, 可以在TextBox的Keypress用常規表示法處理
底下提供一個簡單做法供大大參考:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
string pattern = "";
Regex rgx = new Regex(pattern);
if (!rgx.Match(e.KeyChar.ToString()).Success)
e.Handled = true;
}
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = Convert.ToInt64(textBox1.Text, 16).ToString();
}
}
}
提醒:
1. 要使用常規表示法請記得using System.Text.RegularExpressions;2. Hex String 若超過 Int64, 記得處理Overflow...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div> 本帖最後由 CPX-900TR 於 2023-2-25 02:09 AM 編輯
懇求高手解疑惑!10位數字轉6位數算法?
http://www.eyny.com/thread-13647332-1-1.html
頁:
[1]