c#string字符串轉存為byte[]數組


  • string a = "10 67 65 83 67 65 68 69 32 84 111 112 111 108 111 103 121 32 86 49 44 32 40 99 41 32 77 97 116 114 97 45 68 97 116 97 118 105 115 105 111 110";
  •  
    byte[] arr = a.Split(' ').Select(x => byte.Parse(x)).ToArray();
     
     

      1. //轉回byte數組
      2.  
        int len = (str.Length+1)/3
      3.  
        byte[] byteArr2 = new byte[len]
      4.  
        for(int i=0;i<len;i++)
      5.  
        {
      6.  
        byteArr2[i] = Convert.ToByte(str.Substring(i* 3,2),16)
         
         
         
              public static byte[] GetByteArray(string shex)
                {
                    string[] ssArray = shex.Split(' ');
                    List<byte> bytList = new List<byte>();
                    foreach (var s in ssArray)
                    {
                        //將十六進制的字符串轉換成數值
                        bytList.Add(Convert.ToByte(s, 16));
                    }
                    //返回字節數組
                    return bytList.ToArray();
                }

  • 免責聲明!

    本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



     
    粵ICP備18138465號   © 2018-2025 CODEPRJ.COM