首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >c#将ascii值的字节数组转换为整数数组。

c#将ascii值的字节数组转换为整数数组。
EN

Stack Overflow用户
提问于 2017-02-09 10:52:24
回答 3查看 1.8K关注 0票数 1

我有一个由50字节组成的字节数组,将5整数表示为ascii字符值。每个整数值都表示为10字节:

代码语言:javascript
运行
复制
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的ascii代码,[48..57]0..9数字的ascii代码。

如何将字节数组转换为整数数组(int[] intvalues == [1, 2, 1234, 5801, 999])?

我首先尝试将字节数组转换为字符串,然后将字符串转换为整数,如下所示:

代码语言:javascript
运行
复制
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]);

但是,难道没有更简单的方法将字节数组复制到字符串数组中吗?

EN

回答 3

Stack Overflow用户

发布于 2017-02-09 11:03:01

for循环可以简化书写:

代码语言:javascript
运行
复制
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));
票数 1
EN

Stack Overflow用户

发布于 2017-02-09 11:20:10

我建议使用Linq

  • 在10项(即10-byte)块上拆分初始数组
  • 在每个块中过滤数字('0'.'9')
  • Aggergate数字转换为单个整数

执行情况:

代码语言:javascript
运行
复制
  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();

测试

代码语言:javascript
运行
复制
  Console.Write(string.Join(", ", intvalues));

结果:

代码语言:javascript
运行
复制
  1, 2, 1234, 5801, 999

请注意,10数字可以很好地溢出int,因为最大int值(int.MaxValue)仅为2147483647。要将初始byte[]表示为string,您可以再次使用Linq:

代码语言:javascript
运行
复制
  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);

结果

代码语言:javascript
运行
复制
20202020202020202049
20202020202020202050
20202020202049505152
20202020202053564849
20202020202020575757
票数 0
EN

Stack Overflow用户

发布于 2017-02-09 11:02:53

你可以试试这个:-

代码语言:javascript
运行
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42134733

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档