2016-06-26 89 views
0

我想將string轉換爲byte並將其輸出爲stringC#將字符串轉換爲字節並作爲字符串輸出

例如: 字符串:255 輸出:0xFF

richTextBox1.AppendText(textBox1.Text + " || " + Convert.ToBytes(textBox1.Text) + "\n"); 

我得到System.Byte[],而不是價值。

+0

我想你的意思是你要完全由十進制數字字符串,表示從0到255之間,表示在十六進制(大寫)值的字符串數值轉換與一個「0x」前綴。 –

回答

0

有沒有實現數組來做到這一點。你必須自己寫。類似下面的代碼:

byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6 }; 
      string output = string.Empty; 
      foreach (byte item in bytes) 
      { 
       output += Convert.ToString(item, 16).ToUpper().PadLeft(2,'0'); 

      } 
      Console.WriteLine(output); 
      //or using string.Format 
      bytes = new byte[] { 1, 2, 3, 14, 15, 16 }; 
      output = string.Empty; 
      foreach (byte item in bytes) 
      { 
       output = string.Format("{0},{1:X}", output, item); 

      } 
      Console.WriteLine(output); 
+0

工程就像一個魅力,謝謝 –