Introduction
This article teaches you how to convert an int data type to a byte array using C#.
The BitConverter class in .NET Framework provides functionality to convert base data types to an array of bytes and an array of bytes to base data types. The BitConverter class has a static overloaded GetBytes method that takes an integer, double, or other base type value and converts that to an array of bytes. The BitConverter class also has other static methods to reverse this conversion. These methods are ToDouble, ToChart, ToBoolean, ToInt16, and ToSingle.
BitConverter class in C#
The BitConverter class in C# is a static class that helps in manipulating value types in their fundamental form, as a series of bytes. A byte is defined as an 8-bit unsigned integer. The BitConverter class includes static methods to convert each of the primitive types to and from an array of bytes, as shown in the following table:
Method |
Description |
BitConverter.GetBytes(value) |
Converts the specified value to an array of bytes. |
BitConverter.ToUInt32(bytes, startIndex) |
Converts the specified bytes at the specified start index to a 32-bit unsigned integer. |
BitConverter.ToDouble(bytes, startIndex) |
Converts the specified bytes at the specified start index to a double-precision floating-point number. |
BitConverter.ToString(bytes) |
Converts the specified bytes to a hexadecimal string. |
Here is an example of how to use the BitConverter class to convert an integer to an array of bytes:
byte[] bytes = BitConverter.GetBytes(12345);
This code will create an array of bytes with the value 12345 in it. The first byte in the array will be the least significant byte, and the last byte will be the most significant byte.
Here is an example of how to use the BitConverter class to convert an array of bytes to an integer:
int value = BitConverter.ToInt32(bytes, 0);
This code will convert the first four bytes in the array bytes
to an integer.
The BitConverter class is a useful tool for manipulating value types in their binary form. It can be used to encode and decode data, to read and write data to files, and to perform other tasks that require working with binary data.
Here are some other things to keep in mind about the BitConverter class:
- The BitConverter class is not thread-safe.
- The BitConverter class cannot be used to convert objects to arrays of bytes.
- The BitConverter class cannot be used to convert arrays of bytes to objects.
Converts integer values to a byte array and vice-versa
The following code snippet converts integer values to a byte array and vice-versa.
using System;
namespace BitConverterSample
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Int and byte arrays conversion sample.");
// Create int to a byte array
Int32 i32 = 125;
Console.WriteLine("Int value: " + i32.ToString());
byte[] bytes = ConvertInt32ToByteArray(i32);
Console.WriteLine("Byte array value:");
Console.WriteLine(BitConverter.ToString(bytes));
Console.WriteLine("Byte array back to Int32:");
// Create byte array to Int32
double dValue = ConvertByteArrayToInt32(bytes);
Console.WriteLine(dValue.ToString());
Console.ReadLine();
}
public static byte[] ConvertInt32ToByteArray(Int32 I32)
{
return BitConverter.GetBytes(I32);
}
public static byte[] ConvertIntToByteArray(Int16 I16)
{
return BitConverter.GetBytes(I16);
}
public static byte[] ConvertIntToByteArray(Int64 I64)
{
return BitConverter.GetBytes(I64);
}
public static byte[] ConvertIntToByteArray(int I)
{
return BitConverter.GetBytes(I);
}
public static double ConvertByteArrayToInt32(byte[] b)
{
return BitConverter.ToInt32(b, 0);
}
}
}
Output for the above code
Summary
This article taught us how to convert an Int to a Byte Array in C#.