What is the Convert.ToInt32(byte) method?
Overview
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.
Syntax
Convert.ToInt32(byteValue)
Syntax for converting a Byte to Int32 in C# using Convert.ToInt32(byte)
Parameters
byteValue: This is the byte value we want to convert to a 32-bit signed integer.
Return value
It returns an equivalent 32-bit signed integer of the specified byte value byteValue.
Code example
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());}}
Code explanation
- Lines 7 to 10: We create some byte values.
- Lines 13 to 16: We use the
GetType()method to get the data type of byte values we created. - Lines 19 to 22: We use
Convert.ToInt32()to convert the created byte values toInt32values and print the type of data.