C# 中10进制、16进制及其ASCII码之间转化

2021/9/14 9:34:57

本文主要是介绍C# 中10进制、16进制及其ASCII码之间转化,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

C# 中10进制、16进制及其ASCII码之间转化

            //字符串转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();

            //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") + " ");
            }


这篇关于C# 中10进制、16进制及其ASCII码之间转化的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程