We can use the Convert.ToInt32(byte)
method to convert a byte value to its equivalent 32-bit signed integer.
A byte is in the range of 0
to 255
. An Int32
is in the range of -2,147,483,648
to 2,147,483,647
.
Convert.ToInt32(byteValue)
byteValue
: This is the byte value we want to convert to a 32-bit signed integer.It returns an equivalent 32-bit signed integer of the specified byte value byteValue
.
Let's look at the example below:
using System;class HelloWorld{static void Main(){// create some byte valuesbyte byte1 = 20;byte byte2 = 255; // limit of a byte valuebyte byte3 = 45;byte byte4 = 0;// print the data types of the bytes createdConsole.WriteLine(byte1.GetType());Console.WriteLine(byte2.GetType());Console.WriteLine(byte3.GetType());Console.WriteLine(byte4.GetType());// convert the bytes to Int32 and print resultsConsole.WriteLine(Convert.ToInt32(byte1).GetType());Console.WriteLine(Convert.ToInt32(byte2).GetType());Console.WriteLine(Convert.ToInt32(byte3).GetType());Console.WriteLine(Convert.ToInt32(byte4).GetType());}}
GetType()
method to get the data type of byte values we created.Convert.ToInt32()
to convert the created byte values to Int32
values and print the type of data.