代码如下:
int val16 = Convert.ToInt32(val16_Str, 16);//val16_Str为16进制字符串
2进制、8进制同理
C#的进制转换主要用在串口通讯时候
进制转换是人们利用符号来计数的方法。进制转换由一组数码符号和两个基本因素“基数”与“位权”构成。
基数是指,进位计数制中所采用的数码(数制中用来表示“量”的符号)的个数。位权是指,进位制中每一固定位置对应的单位值。
//字符串转byte 获取字节 string s = "我喜欢7这个数字"; byte[] sarr = System.Text.Encoding.Default.GetBytes(s); int len = sarr.Length; //10数字进制转换成ASCII码 int num = 81; string si = ((char)num).ToString();//Q //10进制byte转换为ASCII码 byte[] a1 = new byte[4]; a1[0] = 66; a1[1] = 68; a1[2] = 72; a1[3] = 84; string ascii = Encoding.Default.GetString(a1); //16进制 16进制转ASCII int intAB = 0x42; string ass = ((char)intAB).ToString(); //16进制转为10进制 int intA = 0x42; string strA = "42"; int A1 = Convert.ToInt32(intA); int A2 = Convert.ToInt32(strA,16); //10进制转为16进制 int B1 = 66; string BB = Convert.ToString(B1,16); string BB2 = B1.ToString("x"); //ASCII 字符转10进制byte string st = "38"; byte[] ba = System.Text.ASCIIEncoding.Default.GetBytes(st); //10进制byte 转化为16进制byte byte[] newbyte = new byte[ba.Length]; for (int i = 0; i < ba.Length; i++) { newbyte[i] = Convert.ToByte(ba[i].ToString("x")); } //ASCII字符串转10进制数 string tr = "385A"; string d = ""; for (int i = 0; i < tr.Length; i++) { int ii = (int)Convert.ToChar(tr.Substring(i, 1)); d = d + " " + ii.ToString();//字符串 } ba = System.Text.ASCIIEncoding.Default.GetBytes(tr);//10进制byte //ASCII字符串转16进制数 string s2 = "385A"; byte[] bad = System.Text.ASCIIEncoding.Default.GetBytes(s2); StringBuilder sb = new StringBuilder(); foreach (byte b in bad) { sb.Append(b.ToString("x") + " "); }
标签:10,进制,16,int,转化,byte,string From: https://www.cnblogs.com/Kirito-Asuna-Yoyi/p/17587357.html