C#中10进制、16进制及其ASCII码之间转化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");
16进制字符串转16进制数组//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") + "");
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论