怎么把16进制的字符串:
string str="68 32 00 32 00 68 0B 71 03 01 00 00 00 61 00 00 01 00 E2 16";保存到byte[] b=new byte[1024];数组中?
或者求一个转换方法();
或者求代码。谁让我最满意的分全给谁。   请大虾帮个小忙!!! 在线等。
主要是我把这个串放入NetworkStream netStream = new NetworkStream(Cck);中用的。
netStream,write();在这里用的。必须16进制。

解决方案 »

  1.   

    List<byte> bytes = new ...
    对str中每一个字符调用:
    byte b = Convert.ToByte(x, 16);
    然后bytes.add(b);
    然后把bytes转换成byte[]...
      

  2.   

    /// <summary>
        /// 字符串转16进制字节数组
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        private static byte[] strToToHexByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }
      

  3.   

    呃,错了,是每两个字符
    Regex regex = new regex(@"((?<data>[0-9A-Fa-f]{2}) )+)");
    Match match = regex.Match(str);
    Group group = match.Groups["data"];
    foreach (Capture capture in group.Captures)
    {
       byte b = Convert.ToByte(capture.Value, 16);
       bytes.Add(b);
    }
    bytes.CopyTo(buffer);
    buffer就是你要的那个byte[]了。
    当然取2个字符,并不一定要正则表达式吧...
      

  4.   

                string str = "68 32 00 32 00 68 0B 71 03 01 00 00 00 61 00 00 01 00 E2 16";
                byte[] data = new byte[1024];
                string[] key = str.Split(' ');            for (int i = 0; i < key.Length; i++)
                {                data[i] = Convert.ToByte(key[i], 16);            }
      

  5.   

    上面的代码改一下:
      string str = "68 32 00 32 00 68 0B 71 03 01 00 00 00 61 00 00 01 00 E2 16";
      string[] key = str.Split(' ');
      byte[] data = new byte[key.Length];  for (int i = 0; i < key.Length; i++)
      {  data[i] = Convert.ToByte(key[i], 16);  }
      

  6.   

    string str = "68 32 00 32 00 68 0B 71 03 01 00 00 00 61 00 00 01 00 E2 16";
    byte[] b = new byte[1024];
    int i = 0;
    foreach (string v in str.Split(' '))
    {
    b[i++] = Convert.ToByte(v, 16);
    }
      

  7.   


        string str = "68 32 00 32 00 68 0B 71 03 01 00 00 00 61 00 00 01 00 E2 16";
        //切成数组
        string[] list = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);    //下面这个可以定义为类里的静态变量 或者 常量
        int[] table = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15 };    byte[] result = new byte[list.Length];
        //循环转换
        for (int i = 0; i < list.Length; i++)
        {
            result[i] = (byte)((table[list[i][0] - 48] << 4) + (table[list[i][1] - 48]));
        }
    这个随便写了写,不是最优的,不过效率不错了.
      

  8.   


     Convert.ToByte 隐藏了太多细节了,你们有时间可以看看源码,如果写2行代码就能完成的事最好别用它
      

  9.   


            void Send(string Txt)
            {
                //byte b = Convert.ToByte(s, 16); 
                NetworkStream netStream = new NetworkStream(Cck);
                //byte[] arr = Encoding.UTF8.GetBytes(text);
                string[] key = Txt.Split(' ');
                byte[] arr=new byte[key.Length];
                for (int i = 0; i < arr.Length;i++ )
                {
                    arr[i] = Convert.ToByte(key[i],16);
                }
                netStream.Write(arr, 0, arr.Length);
                netStream.Flush();
                netStream.Close();
            }
    这样就行了。第一点,你们字符长度定为1024的都不行,第二太繁琐也不行。
      

  10.   

    回答过这几个问题的哥们儿可以看下,这是一个Socket网络编程用到的。
    并且还是连接GPRS设备。Socket建立链接后。通过Write();发送16进制的帧格式遇设备通信的。 所以这样必须发16进Byte数组。谢谢参与。我的问题已解决。