譬如我有一字符串内容是 16进制的
如 st=“ABCD1234”现在要把字符串中的字符 两个一取 放入一byte数组里
如:  byte by;
by[0]=0xAB;
by[1]=0xCD;
by[2]=0x12;
by[3]=0x34;

解决方案 »

  1.   

    string st="68CD1234";
    byte[] cc = new byte [st.Length/2];
    for(int i=0;i<st.Length / 2 ;i++)
    {
    cc[i]=Convert.ToByte( st.Substring(2*i,2),16 );
    }
    我运行过了,可以的
      

  2.   

    public byte[] HexString2Bytes(string hexString)
    {
        try
        {
            if (hexString == null || hexString.Length % 2 != 0) return null;
            byte[] bytes = new byte[hexString.Length / 2];
            for (int i = 0; i < hexString.Length / 2; i++)
            {
                bytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            }
            return bytes;
        }
        catch
        {
            return null;
        }
    }