C# byte - working with byte type in C# (2024)

last modified July 5, 2023

In this article we show how to work with the byte type in C#.

The byte type is an simple, numeric, value type in C#.The byte type is mainly used in IO operations, when working withfiles and network connections.

There are two basic byte types:

keyword range size .NET typesbyte -128 to 127 Signed 8-bit integer System.SBytebyte 0 to 255 Unsigned 8-bit integer System.Byte

The listing shows the keywords, range, size and .NET type for the two bytetypes.

C# byte and sbyte

In the first example, we present the basic attributes of the byte types.

Program.cs

byte val1 = 5;sbyte val2 = -4;Console.WriteLine(val1);Console.WriteLine(val2);Console.WriteLine(val1.GetType());Console.WriteLine(val2.GetType());Console.WriteLine("------------------------");Console.WriteLine(byte.MinValue);Console.WriteLine(byte.MaxValue);Console.WriteLine(sbyte.MinValue);Console.WriteLine(sbyte.MaxValue);Console.WriteLine("------------------------");Console.WriteLine(typeof(byte));Console.WriteLine(typeof(sbyte));Console.WriteLine(default(byte));Console.WriteLine(default(sbyte));

The program program defines byte and sbyte types andprints their attributes.

Console.WriteLine(val1.GetType());Console.WriteLine(val2.GetType());

We can determine the type of a variable with GetType.

Console.WriteLine(byte.MinValue);Console.WriteLine(byte.MaxValue);

The minimum and maximum values are determined with MinValue andMaxValue attributes.

Console.WriteLine(typeof(byte));Console.WriteLine(typeof(sbyte));

The underlying .NET type is determined with typeof keyword.

Console.WriteLine(default(byte));Console.WriteLine(default(sbyte));

The default value of a type is returned with default keyword.

$ dotnet run5-4System.ByteSystem.SByte------------------------0255-128127------------------------System.ByteSystem.SByteSystem.Byte00

C# convert string to bytes

We convert strings to bytes and vice versa with Encoding.

Program.cs

using System.Text;string word = "čerešňa";byte[] data = Encoding.UTF8.GetBytes(word);Console.WriteLine(string.Join(" ", data));string word2 = Encoding.UTF8.GetString(data);Console.WriteLine(word2);

We have a word which contains three accented letters.

byte[] data = Encoding.UTF8.GetBytes(word);

To turn the string into bytes, we use the Encoding.UTF8.GetBytes.

string word2 = Encoding.UTF8.GetString(data);

To get the string from the array of bytes, we use theEncoding.UTF8.GetString method.

$ dotnet run196 141 101 114 101 197 161 197 136 97čerešňa

The word has seven letters. In the array we have ten bytes. This means thatthe three accented letters are represented by two bytes each.

C# write bytes to file

In the next example, we write some data to a text file.

public override void Write(byte[] buffer, int offset, int count);

The FileStream.Write method writes a block of bytes to the filestream. The method takes three parameters: the byte array, the zero-based byteoffset in array from which to begin copying bytes to the stream, and themaximum number of bytes to write.

Program.cs

using System.Text;var path = "words.txt";using FileStream fs = File.Create(path);byte[] data = Encoding.UTF8.GetBytes("falcon\nribbon\ncloud\nwater");fs.Write(data, 0, data.Length);Console.WriteLine("data written to file");

The example writes four words to a file.

using FileStream fs = File.Create(path);

We create a new file with File.Create. The method returns aFileStream.

byte[] data = Encoding.UTF8.GetBytes("falcon\nribbon\ncloud\nwater");

We get the bytes of the text with Encoding.UTF8.GetBytes

fs.Write(data, 0, data.Length);

We write the whole array of bytes to the filestream with Write.

Get HTML page

The HttpClient.GetByteArrayAsync sends a GET request to thespecified Uri and return the response body as a byte array in an asynchronousoperation.

Program.cs

string url = "http://webcode.me";HttpClient client = new HttpClient();byte[] data = await client.GetByteArrayAsync(url);string fname = "index.html";File.WriteAllBytes(fname, data);

We retrieve an HTML page into an array of bytes withGetByteArrayAsync. Then we write the array of bytes into a filewith File.WriteAllBytes.

C# File.ReadAllBytes

The File.ReadAllBytes opens a binary file, reads the contents ofthe file into a byte array, and then closes the file.

Program.cs

var path = "favicon.ico";byte[] data = File.ReadAllBytes(path);int i = 0;foreach (byte c in data){ Console.Write("{0:X2} ", c); i++; if (i % 10 == 0) { Console.WriteLine(); }}

The example reads a favicon.ico binary file. The data is printed to the consolein hexadecimal format.

$ dotnet run00 00 01 00 01 00 10 10 00 00 00 00 00 00 68 05 00 00 16 00 00 00 28 00 00 00 10 00 00 00 20 00 00 00 01 00 08 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 FF FF FF 00 4D 45 3D 00 00 00 00 00 00 00 ...

HttpServer example

An HTTP server writes its data to the output stream of the response object.

Program.cs

using System.Net;using System.Text;using var listener = new HttpListener();listener.Prefixes.Add("http://localhost:8001/");listener.Start();Console.WriteLine("Listening on port 8001...");while (true){ HttpListenerContext context = listener.GetContext(); using HttpListenerResponse resp = context.Response; resp.Headers.Set("Content-Type", "text/plain"); string data = "Hello there!"; byte[] buffer = Encoding.UTF8.GetBytes(data); resp.ContentLength64 = buffer.Length; using Stream ros = resp.OutputStream; ros.Write(buffer, 0, buffer.Length);}

C# has the HttpListener to create simple HTTP servers. Our serversends a text message.

using HttpListenerResponse resp = context.Response;resp.Headers.Set("Content-Type", "text/plain");

In the response headers, we set the Content-Type totext/plain to hint the client what kind of data to expect.

string data = "Hello there!";byte[] buffer = Encoding.UTF8.GetBytes(data);

We transform the message into bytes.

resp.ContentLength64 = buffer.Length;

We set the content length.

using Stream ros = resp.OutputStream;ros.Write(buffer, 0, buffer.Length);

Finally, we write the bytes to the output stream with Write.

$ curl localhost:8001Hello there!

Source

Byte struct - language reference

In this article we have worked with byte type in C#.

Author

My name is Jan Bodnar and I am a passionate programmer with many years ofprogramming experience. I have been writing programming articles since 2007. Sofar, I have written over 1400 articles and 8 e-books. I have over eight years ofexperience in teaching programming.

List all C# tutorials.

C# byte - working with byte type in C# (2024)
Top Articles
Latest Posts
Article information

Author: Nathanael Baumbach

Last Updated:

Views: 5760

Rating: 4.4 / 5 (75 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Nathanael Baumbach

Birthday: 1998-12-02

Address: Apt. 829 751 Glover View, West Orlando, IN 22436

Phone: +901025288581

Job: Internal IT Coordinator

Hobby: Gunsmithing, Motor sports, Flying, Skiing, Hooping, Lego building, Ice skating

Introduction: My name is Nathanael Baumbach, I am a fantastic, nice, victorious, brave, healthy, cute, glorious person who loves writing and wants to share my knowledge and understanding with you.