Unsigned Byte Array with Example in C#

C# Unsigned Byte Array: Here, we are going to learn about the unsigned byte array in C# - how to declare, how to initialize and how to access elements? By IncludeHelp Last updated : April 11, 2023

Unsigned Byte Array in C#

In C#.Net, we can create an unsigned byte array by using byte, byte is used to store only positive values between the range of 0 to 255 (Unsigned 8 bits integer).

It occupies 1-byte memory for each element, if array size is 10, it will take 10 bytes memory.

Declaration of a unsigned byte[]

An unsigned byte[] can be declared using the following ways,

  1. Array declaration with initialization
    Syntax:
    byte[] array_name = { byte1, byte2, byte2, ...};
        
    Example:
    byte[] arr1 = { 0, 100, 120, 210, 255};
    
  2. Array declaration with fixed number of elements
    Syntax:
    byte[] array_name = new byte[value];
        
    Example:
    byte[] arr2 = new byte[5];
    
  3. Array declaration with user input
    Syntax:
    byte[] array_name = new byte[variable];
    
    Example:
    byte[] arr2 = new byte[n];
    

Accessing unsigned byte array's elements

Like other types of arrays – we can access the array elements with its index, index starts with 0 and ends with n-1. Here, n is the total number of array elements.

C# Example of Unsigned Byte Array

Consider the given example – Here, we are declaring 3 arrays with 3 different approaches, initializing the arrays either with default values or user input. To print the array elements, we are using foreach loop, we can also use for or while loop with loop counter to access the array elements.

using System;

namespace Test {
  class Program {
    static void Main(string[] args) {
      //declaring unsigned byte[] & initializing it 
      //with 5 elements
      byte[] arr1 = {0,100,120,210,255};

      //printing all bytes of arr1
      Console.WriteLine("arr1 items...");
      foreach(byte item in arr1) {
        Console.WriteLine(item);
      }

      Console.WriteLine(); //to print a line 

      //declaring array for 5 elements 
      //reading values and assigning to array 
      byte[] arr2 = new byte[5];
      
      //reading values from the user
      for (int loop = 0; loop < 5; loop++) {
        Console.Write("Enter a byte (b/w -128 to 127): ");
        arr2[loop] = byte.Parse(Console.ReadLine());
      }
      
      //printing all bytes of arr2
      Console.WriteLine("arr2 items...");
      foreach(byte item in arr2) {
        Console.WriteLine(item);
      }

      Console.WriteLine(); //to print a line 

      //read value of "n" and declare array for "n" elements
      //reading values and assigning to array 
      Console.Write("Enter length of the array: ");
      int n = int.Parse(Console.ReadLine());
      
      //declaring array for n elements
      byte[] arr3 = new byte[n];
      
      //reading values from the user
      for (int loop = 0; loop < n; loop++) {
        Console.Write("Enter a byte (b/w -128 to 127): ");
        arr3[loop] = byte.Parse(Console.ReadLine());
      }
      
      //printing all bytes of arr3
      Console.WriteLine("arr3 items...");
      foreach(byte item in arr3) {
        Console.WriteLine(item);
      }

      //hit ENTER to exit
      Console.ReadLine();
    }
  }
}

Output

arr1 items...
0
100
120
210
255

Enter a byte (b/w -128 to 127): 0
Enter a byte (b/w -128 to 127): 100
Enter a byte (b/w -128 to 127): 150
Enter a byte (b/w -128 to 127): 200
Enter a byte (b/w -128 to 127): 255
arr2 items...
0
100
150
200
255

Enter length of the array: 3
Enter a byte (b/w -128 to 127): 0
Enter a byte (b/w -128 to 127): 225
Enter a byte (b/w -128 to 127): 255
arr3 items...
0
225
255



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.