2016-06-11 213 views
-2

所以我有一個字符串數組,其中包含字節,我想將它們直接移動到字節數組中。我怎樣才能做到這一點?將字符串數組(字節值)轉換爲字節數組

//for example 
string read="0 1 0 0 0 255 255 255 255"; 
byte[] bytes=null; 
string[] splitted = read.Split(' '); 
     for(int i=0;i<splitted.Count();i++) 
     { 
      int value = Int32.Parse(splitted[i]); 
      bytes[i] = (byte)value; 
     } 
     problem = bytes; 
+0

那麼,你有什麼嘗試? –

+0

請澄清字符串如何包含字節的值 –

+0

@ roryap請參閱更新 –

回答

1

簡單和直接的

string read = "0 1 0 0 0 255 255 255 255"; 
byte[] result = read 
    .Split(' ') // => string[]/IEnumerable<string> 
    .Select(s => byte.Parse(s)) // => IEnumerable<byte> 
    .ToArray(); // => byte[] 
-1

你甚至測試過這個代碼嗎?數組的長度屬性不計數。也宣告你的字節數組時,你必須提供它的大小,你不能改變一個動態數組的大小,或者你列出的開始,如果你不知道的長度

+0

這不是一個答案。它屬於OP問題下的評論部分。 –

相關問題