C#中ASCII码学习心得

1.利用调用ASCIIEncoding类来实现各种转换。如简单个ACS码和int转换。

***利用(int)ASCIIEncoding类对象.GetBytes(character)[0]得到整数;

public static int Asc(string character)
        {
            if (character.Length == 1)
            { System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
                int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];
                return (intAsciiCode);
               
            }
            else
            {
                throw new Exception("Character is not valid.");
              
            }
        }

****ASCIIEncoding类对象.GetString(byteArray);

   public static string Chr(int asciiCode)
        {
            if (asciiCode >= 0 && asciiCode <= 255)
            {
                ASCIIEncoding asciiEncoding = new ASCIIEncoding();
                byte[] byteArray = new byte[] { (byte)asciiCode };
                string strCharacter = asciiEncoding.GetString(byteArray);
                return (strCharacter);
            }
            else
            {
                throw new Exception("ASCII Code is not valid.");
            }
        }

//另一种写法
string str="abcd";
byte[] bytetest = System.Text.Encoding.Default.GetBytes(str.ToString());

 有时候为了直观起见及机台(尤其做自动化设备的)需整体传送和解码的作用。

1.将字符转换为对应的ASCII:

string str = textBox1.Text.Trim();   // 去掉字符串首尾处的空格
char[] charBuf = str.ToArray();    // 将字符串转换为字符数组
ASCIIEncoding charToASCII = new ASCIIEncoding();

byte[] TxdBuf = new byte[charBuf.Length];    // 定义发送缓冲区;
TxdBuf = charToASCII.GetBytes(charBuf);    // 转换为各字符对应的ASCII

2、将ASCII转换为对应字符:

byte[] RxdBuf = new byte[len];        //  定义接收缓冲区;

 

for(int i = 0; i < len; i++)

 

{

 

  ASCIIEncoding ASCIITochar = new ASCIIEncoding();
  char[] ascii = ASCIITochar.GetChars(RxdBuf);      // 将接收字节解码为ASCII字符数组
  textBox1.Text += ascii[i];

 

}

1.请问c#中如何将十进制数的字符串转化成十六进制数的字符串

//十进制转二进制
Console.WriteLine("十进制166的二进制表示: "+Convert.ToString(166, 2));
//十进制转八进制
Console.WriteLine("十进制166的八进制表示: "+Convert.ToString(166, 8));
//十进制转十六进制
Console.WriteLine("十进制166的十六进制表示: "+Convert.ToString(166, 16));
   
//二进制转十进制
Console.WriteLine("二进制 111101 的十进制表示: "+Convert.ToInt32("111101", 2));
//八进制转十进制
Console.WriteLine("八进制 44 的十进制表示: "+Convert.ToInt32("44", 8));
//十六进制转十进制
Console.WriteLine("十六进制 CC的十进制表示: "+Convert.ToInt32("CC", 16));

2.在串口通讯过程中,经常要用到 16进制与字符串、字节数组之间的转换
//

private string StringToHexString(string s,Encoding encode)
        {
            byte[] b = encode.GetBytes(s);//按照指定编码将string编程字节数组
            string result = string.Empty;
            for (int i = 0; i < b.Length; i++)//逐字节变为16进制字符,以%隔开
            {
                result += "%"+Convert.ToString(b[i], 16);
            }
            return result;
        }
        private string HexStringToString(string hs, Encoding encode)
        {
            //以%分割字符串,并去掉空字符
            string[] chars = hs.Split(new char[]{'%'},StringSplitOptions.RemoveEmptyEntries);
            byte[] b = new byte[chars.Length];
            //逐个字符变为16进制字节数据
            for (int i = 0; i < chars.Length; i++)
            {
                b[i] = Convert.ToByte(chars[i], 16);
            }
            //按照指定编码将字节数组变为字符串
            return encode.GetString(b);
        }

 

posted on 2017-07-11 11:24  努力的毛毛虫  阅读(1087)  评论(0编辑  收藏  举报

导航