2010-12-07 127 views
0

哪有我的值設置爲一個byte []值分配給在C#類型的byte []

我曾嘗試以下,但我得到一個編譯錯誤:

byte[] XMLbyte=null; 
XMLbyte = byte(2345) 

任何建議如何我可以做到這一點?

+6

究竟是什麼你想怎麼辦? – 2010-12-07 08:02:31

回答

0

byte[]是一個字節數組。您可能只需要byte類型。

0

好吧,看來很傻,但我認爲你缺少的操作

XMLbyte = new byte[2345] 
1

一些選項來初始化數組:

XMLbyte = new byte[50]; // creates an array with 50 elements, all values are zero. 
XMLbyte = new byte[3] {1,2,3}; // creates an array with 3 elements: 1,2,3 
XMLbyte = new byte[] {1,2,3}; // creates an array with 3 elements: 1,2,3 
XMLbyte = {1,2,3}; // creates an array with 3 elements: 1,2,3 
0

您正在尋找GetBytes方法:

byte[] XMLbyte = BitConverter.GetBytes(2345); 
+0

我相信你已經走得太遠了。我嚴重懷疑他的意圖是獲得代表整數的4個字節。 – 2010-12-07 08:14:59

0

請嘗試下面的代碼

byte[] XMLbyte=null; 
XMLbyte = new byte[2] {1,2} 
3

您必須XMLbyte = new byte [2345];

也許你想從「2345」字符串的bytearray?

System.Text.UTF8Encoding encoding=new System.Text.UTF8Encoding(); 
XMLbyte = encoding.GetBytes("2345"); 
0

試試這個:byte[] temp = new byte [255];

或者這樣:byte[] temp = new byte [123];

相關問題