[英]c# convert byte array of ascii values to integer array
我有一個50
字節的字節數組 ,表示5
整數作為ascii字符值。 每個整數值表示為10
個字節:
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'
請注意, 20
是space
的ascii代碼, [48..57]
是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]);
但是沒有更簡單的方法將字節數組復制到字符串數組中嗎?
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));
我建議使用Linq :
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
你可以試試這個: -
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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.