我想向串口发数据
比如在输入框中输入AA BB
发送的时候怎么将它转成16进制形式呢
像串口调试助手一样

解决方案 »

  1.   

    string data = "AA BB";
    string[] arr = data.Split(' ');
    byte[] buf = new byte[arr.Length];
    for (int i = 0; i < buf.Length; i++)
    {
    buf[i] = Convert.ToByte(arr[i], 16);
    }
      

  2.   

    using System.Globalization;int.Parse(string,NumberStyles.HexNumber);
      

  3.   

    string hex = "AA bb 1C 9f";
    List<byte> data = new List<byte>();
    Regex.Replace(hex,@"[a-fA-F0-9]{2}",delegate(Match m)
    {
        data.Add(byte.Parse(m.Value, System.Globalization.NumberStyles.HexNumber));
        return "";
    });
    //data就是你要的结果
    //如果需要数组
    //data.ToArray();
      

  4.   

    我把问题描述错误了
    我是想这样做
    当输入框中输入 AA BB的时候
    怎么将这个AA BB放入一个byte[] 数组中呢,因为向串口发送指令是这个类型的啊
      

  5.   

    你不觉得4楼贴的就是你要的么?data就是byte数组,莫非?
    你不会吧
    string hex = "AA bb 1C 9f";
    这句换成
    string hex = Textbox1.Text;?
      

  6.   

    让它在数组中变成这种形式byte [] b={0xAA,0xBB};