1

I have a byte array of 50 bytes representing 5 integers as ascii characters values. Every integer value is represented as 10 bytes:

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'

Please, notice that 20 is an ascii code of space and [48..57] are ascii codes of 0..9 digits.

How can I convert the byte array to an integer array (int[] intvalues == [1, 2, 1234, 5801, 999])?

I have tried first to convert byte array to string and then string to integer like this:

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]);

But isn't there a simpler way to copy the byte array into the string array?

2

3 Answers 3

1

A for loop can simplify the writing:

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));
Sign up to request clarification or add additional context in comments.

Comments

0

I suggest using Linq:

  • Split initial array on 10-items (i.e. 10-byte) chunks
  • Filter digits ('0'..'9' ) in each chunk
  • Aggergate digits into a single integer

Implementation:

  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();

Test

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

Outcome:

  1, 2, 1234, 5801, 999

Please, notice, that 10 digits number can well overflow int since maximum int value (int.MaxValue) is 2147483647 only. To represent the initial byte[] as a string you can use Linq once again:

  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);

Outcome

20202020202020202049
20202020202020202050
20202020202049505152
20202020202053564849
20202020202020575757

2 Comments

To be honest, I find the OP's initial code (Encoding.ASCII.GetString followed by int.Parse) much easier to read & understand.
@stakx: it depends on what bytes can appear in the initial receiveBytes array; if digits and spaces only then I agree. If, say, it can have 160 (non-breaking space), 95 (underscope), 0 ('\0') etc. as placeholders then I'd rather stick to Where + Aggregate
-1

You can try this :-

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);
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.