例如  txtbox里内容为“01ac0c01”
每两位的存入byte数组。得到的为byte【】a={01,ac,0c,01};
该如何完成,谢谢

解决方案 »

  1.   

    string s = "01ac0c01";
    MatchCollection matches = Regex.Matches(s, @"[0-9A-Fa-f]{2}");
    byte[] bytes = new byte[matches.Count];
    for (int i = 0; i < bytes.Length; i++)
    bytes[i] = byte.Parse(matches[i].Value, NumberStyles.AllowHexSpecifier);
      

  2.   

                string ss = "01ac0c01";
                int len = ss.Length / 2;
                byte[] bs = new byte[len];
                int j = 0;
                for (int i = 0; i < ss.Length; i += 2)
                {
                    string str = ss.Substring(i, 2);
                    bs[j] = (byte)int.Parse(str, System.Globalization.NumberStyles.HexNumber);
                    j++;
                }