string "112233"如何转成 new byte[] { 0x11, 0x22, 0x33}

解决方案 »

  1.   

    // using System.Text.RegularExpressions;
    string s = "112233";
    byte[] result = Regex.Matches(s, "\\d{2}").Cast<Match>().Select(x => (byte)int.Parse("0x" + x.Value)).ToArray();
      

  2.   

    是这样吗txt_neirong.Text = null;
                        string abc = "123456";
                        byte[] abc2= Encoding.UTF8.GetBytes(abc);
      

  3.   

    复制错了, txt_neirong.Text = null; 这个是其他地方的,多复制了
      

  4.   

    string abc = "123456";                    
     byte[] abc2= Encoding.UTF8.GetBytes(abc);+++++
      

  5.   

    =
    报错啊,没有Cast<Match>()这个方法
      

  6.   


    引用这个做出来的软件要安装NET3.0以上的版本,我小软件觉得这样不方便xp用户,还有其他办法吗?
      

  7.   

    string s = "112233";
    List<byte> list = new List<byte>();
    foreach (var x in Regex.Matches(s, "\\d{2}"))
        list.Add((byte)int.Parse("0x" + x.Value));
    byte[] result = new byte[list.Count];
    for (int i = 0; i < list.Count - 1; i++)
    {
        result[i] = list[i];
    }
      

  8.   


    string str = "112233";
                List<byte> bt = new List<byte>();
                foreach (Match m in Regex.Matches(str, @"[a-fA-F0-9]{2}"))
                {
                    bt.Add((byte)Convert.ToInt32(m.Value, 16));
                }
    return bt;
      

  9.   


    如果你的源中有abcdef那么把\\d{2}修改成ls的形式。