如题。举例如何把  byte[] crc ={ 0x2e, 0x4a, 0xbd, 0x10 };转换成string stra = "2e4abd10";谢谢。在线等。

解决方案 »

  1.   

    string s;
    for (i=0;i<4;i++)
    {
    s += byte[i].ToString("x");
    }
      

  2.   


    谢谢,我刚才就用这个代码调试成功了。可以再问下吗?就是如果反过来把那个转成字节数组怎么转呢【因为程序中别的地方用到】。比如把string stra = "2e4abd10";转换成
     byte[] crc ={ 0x2e, 0x4a, 0xbd, 0x10 };
      

  3.   

    反过来只能自己写代码了。        static void Main(string[] args)
            {
                string stra = "2e4abd10";            byte[] result = ParseByteArray(stra);
            }        static byte[] ParseByteArray(string str)
            {
                int arrayLength = str.Length / 2;
                byte[] byteArray = new byte[arrayLength];            for (int i = 0; i < arrayLength; ++i)
                {
                    byteArray[i] = byte.Parse(str.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber);
                }            return byteArray;
            }
      

  4.   

                string hexString = "2e";
                int num = Int32.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
      

  5.   

    可是这样跟踪断点得到的结果是
    result[0]、result[1]、result[2]、result[3]
    分别是46、74、189、16也不是我想实现的这种格式啊
    转换成
     byte[] crc ={ 0x2e, 0x4a, 0xbd, 0x10 };
      

  6.   


    好意思,我真的不懂,就是跟踪出来的结果是46,不是 byte[] crc ={ 0x2e}这种格式。
      

  7.   

    我晕,byte本来保存的就是一个数字啊。46和0x2e只是输出为string的格式是十进制还是十六进制。运行下面的程序,结果就是你想要的。        static void Main(string[] args)
            {
                string stra = "2e4abd10";            byte[] result = ParseByteArray(stra);
                PrintByteArray(result);
            }        static byte[] ParseByteArray(string str)
            {
                int arrayLength = str.Length / 2;
                byte[] byteArray = new byte[arrayLength];            for (int i = 0; i < arrayLength; ++i)
                {
                    byteArray[i] = byte.Parse(str.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber);
                }            return byteArray;
            }        static void PrintByteArray(byte[] byteArray)
            {
                Console.Write("{");            foreach (byte item in byteArray)
                {
                    Console.Write(" 0x" + item.ToString("x02"));
                }            Console.WriteLine(" }");
            }
      

  8.   

     
     byte[] inputBytes; 
    string inputString = System.Convert.ToBase64String(inputBytes);