我现在做的是串口通信  ,我想在通信时以ASCII码的形式传送数据,
比如我在一个TEXTBox中输入一组16进制的数据:12 2C 32 ... 中间以空格隔开
我想让每个字节都表示为两个ASCII码组成,比如2C可表示为ASCII码的32H 43H,32可表示为33H 32H,
就是把这组数据转换为ASCII的16进制数!!让一个字节变为两个字节表示!!该怎么办啊??请教高手!!!!

解决方案 »

  1.   


    string str = xxTextBox.text;
    str.Replace(" ", "");//去除中间的空格
    byte[] bytes = new byte[str.Length];
    Encoding.ASCII.GetBytes(str, 0, str.Length, bytes, 0);
    foreach (byte b in bytes)
    {
      Console.WriteLine(Convert.ToString(b, 16) + "H");
    }
      

  2.   

    是不是只需要一个Encoding.ASCII.GetBytes就行啦 ???
      

  3.   

    string str = xxTextBox.text;
    str.Replace(" ", "");//去除中间的空格
    byte[] bytes = new byte[str.Length];
    Encoding.ASCII.GetBytes(str, 0, str.Length, bytes, 0);
    foreach (byte b in bytes)
    {
      Console.WriteLine(Convert.ToString(b, 16) + "H ");
    }
      

  4.   


                string str = "12 2C 32";
                string[] temp = str.Split(' ');
                foreach (string s in temp)
                {
                    foreach (byte b in Encoding.ASCII.GetBytes(s.ToCharArray()))
                    {
                        Console.WriteLine(Convert.ToString(b, 16) + "H");
                    }
                }