2017-02-09 164 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' –