c sharp

How to Understand and Use C# Byte Type

C#, like most programming languages, support a wide array of data types which are the building block of efficient development. Understanding the supported data types, their features, memory layout, size, and other features can help you in creating the performant and safe applications.

In this tutorial, we will learn about the byte data type in C#. Bytes play a crucial role in storing the binary data such as reading the files, etc.

C# Byte Type

A byte in C# is a simple and numeric value data type. One of the major use of a byte type is IO operations, especially when you need to read the files or process the networked data.

A byte can store the values from 0 to 255 as unsigned integer values. This makes it very efficient in memory.

There are two main types of byte values in C#:

a. Byte – The first type is the byte which is an unsigned 8-bit integer that can store the values from 0 to 255.
b. Sbyte – The second type is an sbyte which is a signed 8-bit integer that can store the values that range from -128 to 127.

In C#, we can declare a byte variable as shown in the following example syntax:

byte var = value;

Take the following example:

byte bt = 25;
Console.WriteLine(bt);

The previous code declares an initialized variable called “bt” of byte type and stores the integer 25.

Byte Array

We can also declare an array that stores multiple byte values which is also known as byte array. An example is as follows:

byte[] byteArray = {34, 1, 10, 5, 255};
foreach(var b in byteArray)
{
    Console.WriteLine(b);
}

In the example, we declare and initialize a byte array with each valid representing a valid byte value. We then use a “foreach” loop to iterate over each element in the array and print the result to the console.

Byte Cyclic

Bytes contain a property that is known as cyclic form. This means that once a byte reaches the maximum value it can store, it reverts back to 0 as shown in the following example:

byte v = 255;
v++;
Console.WriteLine(v);

In the given example, we have a byte variable and initialize its value to 255. Since a byte can hold up to 255, attempting to increment the value causes it to overflow and start from 0.

Convert a String to Byte

One of the most used type conversion in C# is converting a string into a byte value. For that, we can use the “Encoding” class which provides us with various methods to convert a string into a byte array and vice versa.

Common conversion methods include:

a. UTF8.GetBytes(str) – It allows us to convert a UTF-8 encoded string to a byte array.
b. ASCII.GetBytes(str) – It converts an ASCII string to a byte array.
c. Unicode.GetBytes(str) – It converts the Unicode string to a byte array.

Example usage:

using System;
using System.Text;

class Program
{
    static void Main()
    {
        string str = "https://linuxhint.com!";

        byte[] utf8Bytes = Encoding.UTF8.GetBytes(str);
        Console.WriteLine("UTF-8: ");
        foreach (var b in utf8Bytes)
        {
            Console.Write(b + " ");
        }

        Console.WriteLine("\n");

        byte[] asciiBytes = Encoding.ASCII.GetBytes(str);
        Console.WriteLine("ASCII: ");
        foreach (var b in asciiBytes)
        {
            Console.Write(b + " ");
        }

        Console.WriteLine("\n");

        byte[] unicodeBytes = Encoding.Unicode.GetBytes(str);
        Console.WriteLine("Unicode: ");
        foreach (var b in unicodeBytes)
        {
            Console.Write(b + " ");
        }
        Console.WriteLine();
    }
}

The previous example shows how to use the encoding methods to convert a string into various encoded byte arrays.

UTF-8:
104 116 116 112 115 58 47 47 108 105 110 117 120 104 105 110 116 46 ..
ASCII:
104 116 116 112 115 58 47 47 108 105 110 117 120 104 105 110 116 46 ..
Unicode:
104 0 116 0 116 0 112

Conclusion

In this tutorial, we explored the fundamentals of working with the byte data type in C#.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list