2017-02-09 415 views
1

我有一個字節數組50字節表示5整數ascii字符值。每個整數值被表示爲10字節:c#將ascii值的字節數組轉換爲整型數組

byte[] receiveBytes = new byte[] { 
    20, 20, 20, 20, 20, 20, 20, 20, 20, 49, // 9 spaces then '1' 
    20, 20, 20, 20, 20, 20, 20, 20, 20, 50, // 9 spaces then '2' 
    20, 20, 20, 20, 20, 20, 49, 50, 51, 52, // 6 spaces then '1' '2' '3' '4' 
    20, 20, 20, 20, 20, 20, 53, 56, 48, 49, // 6 spaces then '5' '8' '0' '1' 
    20, 20, 20, 20, 20, 20, 20, 57, 57, 57}; // 7 spaces then '9' '9' '9' 

請,通知,20space[48..57]一個ASCII碼是0..9位ASCII碼。

如何將字節數組轉換爲整型數組int[] intvalues == [1, 2, 1234, 5801, 999])?

我也第一次嘗試到字節數組轉換爲字符串,然後串到整數這樣的:

string[] asciival = new string[10]; 
int[] intvalues = new int[5]; 

Byte[] receiveBytes = '20202020202020202049 //int value = 1 
         20202020202020202050 //int value = 2 
         20202020202049505152 //int value = 1234 
         20202020202053564849 //int value =5801 
         20202020202020575757';//int value = 999 

asciival[0] = Encoding.ASCII.GetString(receiveBytes, 0, 10); 
asciival[1] = Encoding.ASCII.GetString(receiveBytes, 10, 10); 

intvalues[0] = int.Parse(asciival[0]); 
intvalues[1] = int.Parse(asciival[1]); 

但是是不是有一個更簡單的字節數組複製到字符串數組的方式?

+0

也許與http://stackoverflow.com/questions/6165171/convert-byte-複製array-to-int – GSP

+2

如何製作10個字節寬的int? 'sizeof(int)== 4' –

回答

-1

你可以試試這個: -

using System; 
using System.Text; 
class Example 
{ 
    public static void Main() 
    { 
    // Define a string. 
    String original = "ASCII Encoding"; 
// Instantiate an ASCII encoding object. 
    ASCIIEncoding ascii = new ASCIIEncoding(); 
// Create an ASCII byte array. 
    Byte[] bytes = ascii.GetBytes(original); 
// Display encoded bytes. 
    Console.Write("Encoded bytes (in hex): "); 
    foreach (var value in bytes) 
    Console.Write("{0:X2} ", value); 
    Console.WriteLine(); // Decode the bytes and display the resulting Unicode string. 
    String decoded = ascii.GetString(bytes); 
    Console.WriteLine("Decoded string: '{0}'", decoded); 
    } 
} 
1

for循環可以簡化書寫:

byte[] recv = new byte[]{ /* ... */ } 

int[] intvalues = new int[recv.Length/10]; 

for(int pos = 0; pos < recv.Length; pos += 10) 
    intvalues[pos/10] = int.Parse(Encoding.ASCII.GetString(recv, pos, pos + 10)); 
0

我建議使用的Linq

  • 拆分初始陣列上10 - 項目(即10- byte)塊
  • 過濾位數('0' .. '9')在每個組塊
  • Aggergate位數成一個單一的整數

實現:

using System.Linq; 
    ... 

    Byte[] receiveBytes = new byte[] { 
    20, 20, 20, 20, 20, 20, 20, 20, 20, 49, // 9 spaces then '1' 
    20, 20, 20, 20, 20, 20, 20, 20, 20, 50, // 9 spaces then '2' 
    20, 20, 20, 20, 20, 20, 49, 50, 51, 52, // 6 spaces then '1' '2' '3' '4' 
    20, 20, 20, 20, 20, 20, 53, 56, 48, 49, // 6 spaces then '5' '8' '0' '1' 
    20, 20, 20, 20, 20, 20, 20, 57, 57, 57}; // 7 spaces then '9' '9' '9' 

    int[] intvalues = Enumerable.Range(0, receiveBytes.Length/10) 
    .Select(index => receiveBytes 
     .Skip(index * 10) // Skip + Take: splitting on 10-items chunks 
     .Take(10)     
     .Where(b => b >= '0' && b <= '9') // just digits 
     .Aggregate(0, (s, a) => s * 10 + a - '0')) 
    .ToArray(); 

測試

Console.Write(string.Join(", ", intvalues)); 

結果:

1, 2, 1234, 5801, 999 

請,通知,即10位數能很好溢出int以來最大int值(int.MaxValue)僅2147483647。爲了表示初始byte[]string你可以使用LINQ的再次:

var result = Enumerable 
    .Range(0, receiveBytes.Length/10) 
    .Select(index => receiveBytes 
     .Skip(index * 10) // Skip + Take: splitting on 10-items chunks 
     .Take(10) 
     .Select(b => b.ToString("00"))) // enforce leading "0" if necessary 
    .Select(items => string.Concat(items)); 

    string text = string.Join(Environment.NewLine, result); 

    Console.Write(text); 

成果

20202020202020202049 
20202020202020202050 
20202020202049505152 
20202020202053564849 
20202020202020575757 
+0

感謝德米特里,這很好。 –

+0

說實話,我發現OP的初始代碼('Encoding.ASCII.GetString'後面加上'int.Parse')更容易閱讀和理解。 – stakx

+0

@stakx:它取決於哪些字節可以出現在最初的'receiveBytes'數組中;如果只有數字和空格,我同意。如果說,它可以具有'160'(非中斷空格),'95'(低視角),'0'(''\ 0'')等佔位符,那麼我寧願堅持'Where' + 'Aggregate' –