二进制-八进制-十进制-16进制之间的转换
生活随笔
收集整理的這篇文章主要介紹了
二进制-八进制-十进制-16进制之间的转换
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?
進(jìn)制之間的轉(zhuǎn)換
主要用到連個(gè)函數(shù):
Convert.ToInt32(str,numbased):將字符串轉(zhuǎn)成str,按照numbeased指定的進(jìn)制轉(zhuǎn)成10進(jìn)制數(shù);Convert.ToString(decimalNum,numbased):將十進(jìn)制數(shù)decimalNum轉(zhuǎn)成numberbased指定的進(jìn)制字符串
十進(jìn)制轉(zhuǎn) 二進(jìn)制,八進(jìn)制,16進(jìn)制
//10進(jìn)制轉(zhuǎn)成二進(jìn)制 Convert.ToString(999,2);//10進(jìn)制轉(zhuǎn)成8進(jìn)制 Convert.ToString(999,8);//10進(jìn)制轉(zhuǎn)16進(jìn)制 Convert.ToString(999,16);//或者 string.Format("{0:X}", 999);二進(jìn)制 轉(zhuǎn) 8進(jìn)制 ,10進(jìn)制,16進(jìn)制
// 2 進(jìn)制轉(zhuǎn) 8進(jìn)制//假如二進(jìn)制是字符串 strb string strb="0101111"; int tmp =Convert.ToInt32(strb,2);//按照2進(jìn)制解析成10進(jìn)制數(shù) //使用10進(jìn)制轉(zhuǎn)成其他進(jìn)制來(lái)進(jìn)行轉(zhuǎn)換 Convert.ToString(tmp,8);//2 進(jìn)制轉(zhuǎn)10,16進(jìn)制都是這樣推導(dǎo)的8進(jìn)制轉(zhuǎn) 2進(jìn)制 10進(jìn)制 ,16進(jìn)制的方法同上
16進(jìn)制 轉(zhuǎn) 2進(jìn)制 8進(jìn)制10進(jìn)制? 同上
?
當(dāng)對(duì)字符串轉(zhuǎn)成其他進(jìn)制的過(guò)程中,需要對(duì)對(duì)應(yīng)進(jìn)制判斷是否符合進(jìn)制數(shù)的要求:
使用正則來(lái)校驗(yàn) 進(jìn)制數(shù)字符串
//判斷是否是二進(jìn)制 public static bool IsBinary(string str) {return Regex.IsMatch(str, "^[0-1]+$"); } //判斷是否是八進(jìn)制數(shù) public static bool IsOctal(string str) {return Regex.IsMatch(str, "^[0-7]+$"); } //判斷是否十進(jìn)制數(shù) public static bool IsDecimal(string str) {return Regex.IsMatch(str, "^[0-9]+$"); } //判斷是否十六進(jìn)制數(shù) public static bool IsHexDecimal(string str) {return Regex.IsMatch(str, "^[0-9A-Fa-f]+$"); }?
下面進(jìn)入正題:效果圖
?
?
窗體代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; //正則校驗(yàn) using System.Text.RegularExpressions;namespace Conversion {public partial class Form1 : Form{public Form1(){InitializeComponent();}private void btnConvert_Click(object sender, EventArgs e){Action();}//進(jìn)制轉(zhuǎn)換實(shí)現(xiàn)方法private void Action(){//獲取輸入string input_str = textBoxInput.Text;switch(comboBoxBerforBase.SelectedIndex){case 0://二進(jìn)制轉(zhuǎn)其他進(jìn)制//根據(jù)選擇自動(dòng)的進(jìn)行轉(zhuǎn)換if(Transform.IsBinary(input_str)){textBoxResult.Text = new Transform().BinaryTo(input_str, comboBoxTargetBase.SelectedIndex);}else{MessageBox.Show("輸入的數(shù)字不是有效的二進(jìn)制數(shù)", "提示");return;}break;case 1:if (Transform.IsOctal(input_str)){textBoxResult.Text = new Transform().OctalTo(input_str, comboBoxTargetBase.SelectedIndex);}else{MessageBox.Show("輸入的數(shù)字不是有效的八進(jìn)制數(shù)", "提示");return;}break;case 2:if (Transform.IsDecimal(input_str)){textBoxResult.Text = new Transform().DecimalTo(input_str, comboBoxTargetBase.SelectedIndex);}else{MessageBox.Show("輸入的數(shù)字不是有效10進(jìn)制數(shù)", "提示");return;}break;case 3:if (Transform.IsHexDecimal(input_str)){textBoxResult.Text = new Transform().HexDecimallTo(input_str, comboBoxTargetBase.SelectedIndex);}else{MessageBox.Show("輸入的數(shù)字不是有效16進(jìn)制數(shù)", "提示");return;}break; }}} }新建TransForm類:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;//正則校驗(yàn) using System.Text.RegularExpressions; namespace Conversion {//使用反射應(yīng)該更好===后期學(xué)完其他知識(shí)后,更新一下優(yōu)化的代碼class Transform{//判斷是否是二進(jìn)制public static bool IsBinary(string str){return Regex.IsMatch(str, "^[0-1]+$");}//判斷是否是八進(jìn)制數(shù)public static bool IsOctal(string str){return Regex.IsMatch(str, "^[0-7]+$");}//判斷是否十進(jìn)制數(shù)public static bool IsDecimal(string str){return Regex.IsMatch(str, "^[0-9]+$");}//判斷是否十六進(jìn)制數(shù)public static bool IsHexDecimal(string str){return Regex.IsMatch(str, "^[0-9A-Fa-f]+$");}//二進(jìn)制轉(zhuǎn)其他進(jìn)制 2 --》 2 8 10 16public string BinaryTo(string str,int NumBased){string result = "";if(IsBinary(str))//如果是二進(jìn)制{switch(NumBased){case 0://轉(zhuǎn)換成本身default://不在范圍內(nèi)就原先字符碼顯示result = str;break;case 1://轉(zhuǎn)成8進(jìn)制int tmp = Convert.ToInt32(str, 2);//十進(jìn)制result = Convert.ToString(tmp,8);//十進(jìn)制--》8進(jìn)制break;case 2://轉(zhuǎn)成10進(jìn)制result = Convert.ToInt32(str,2).ToString();break;case 3://轉(zhuǎn)成16進(jìn)制result = string.Format("{0:X}", Convert.ToInt32(str, 2));break;}}return result;}//8進(jìn)制轉(zhuǎn)其他進(jìn)制 8 --》 2 8 10 16public string OctalTo(string str, int NumBased){string result = "";if (IsOctal(str))//如果是8進(jìn)制{switch (NumBased){case 1://轉(zhuǎn)換成本身default://不在范圍內(nèi)就原先字符碼顯示result = str;break;case 0://轉(zhuǎn)成2進(jìn)制int tmp = Convert.ToInt32(str, 8);//十進(jìn)制result = Convert.ToString(tmp, 2);//十進(jìn)制--》2進(jìn)制break;case 2://轉(zhuǎn)成10進(jìn)制result = Convert.ToInt32(str, 8).ToString();break;case 3://轉(zhuǎn)成16進(jìn)制result = string.Format("{0:X}", Convert.ToInt32(str, 8));break;}}return result;}//10進(jìn)制轉(zhuǎn)其他進(jìn)制 10--》 2 8 10 16public string DecimalTo(string str, int NumBased){string result = "";if (IsDecimal(str))//如果是8進(jìn)制{switch (NumBased){case 2://轉(zhuǎn)換成本身default://不在范圍內(nèi)就原先字符碼顯示result = str;break;case 0://轉(zhuǎn)成2進(jìn)制int tmp_ = Convert.ToInt32(str, 10);//十進(jìn)制result = Convert.ToString(tmp_, 2);//十進(jìn)制--》2進(jìn)制break;case 1://轉(zhuǎn)成8進(jìn)制int tmp_1 = Convert.ToInt32(str, 10);result = Convert.ToString(tmp_1, 8);break;case 3://轉(zhuǎn)成16進(jìn)制result = string.Format("{0:X}", Convert.ToInt32(str, 10));break;}}return result;}//16進(jìn)制轉(zhuǎn)其他進(jìn)制 16--》 2 8 10 16public string HexDecimallTo(string str, int NumBased){string result = "";if (IsHexDecimal(str))//如果是16進(jìn)制{switch (NumBased){case 3://轉(zhuǎn)換成本身default://不在范圍內(nèi)就原先字符碼顯示result = str;break;case 0://轉(zhuǎn)成2進(jìn)制int tmp_ = Convert.ToInt32(str, 16);//十進(jìn)制result = Convert.ToString(tmp_, 2);//十進(jìn)制--》2進(jìn)制break;case 1://轉(zhuǎn)成8進(jìn)制int tmp_1 = Convert.ToInt32(str, 16);result = Convert.ToString(tmp_1, 8);break;case 2://轉(zhuǎn)成10進(jìn)制result = Convert.ToInt32(str, 16).ToString();break;}}return result;}} }
?
轉(zhuǎn)載于:https://www.cnblogs.com/ssjt/articles/10119683.html
總結(jié)
以上是生活随笔為你收集整理的二进制-八进制-十进制-16进制之间的转换的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Rust错误处理
- 下一篇: 变频电源外围配置的全面要点