BitConverter Class in C#


The BitConverter class converts base data types to an array of bytes, and an array of bytes to base data types.

Following are the methods −

MethodDescription
DoubleToInt64Bits(Double)Converts the specified double-precision floating-point number to a 64-bit signed integer.
GetBytes(Boolean)Returns the specified Boolean value as a byte array.
GetBytes(Char)Returns the specified Unicode character value as an array of bytes.
GetBytes(Double)Returns the specified double-precision floating-point value as an array of bytes.
GetBytes(Int16)Returns the specified 16-bit signed integer value as an array of bytes.
GetBytes(Int32)Returns the specified 32-bit signed integer value as an array of bytes.
Int64BitsToDouble(Int64)Reinterprets the specified 64-bit signed integer to a double-precision floating-point number.
ToBoolean(Byte[], Int32)Returns a Boolean value converted from the byte at a specified position in a byte array.
ToChar(Byte[], Int32)Returns a Unicode character converted from two bytes at a specified position in a byte array.
ToString(Byte[])Converts the numeric value of each element of a specified array of bytes to its equivalent hexadecimal string representation.
ToString(Byte[], Int32)Converts the numeric value of each element of a specified subarray of bytes to its equivalent hexadecimal string representation.
ToString(Byte[], Int32, Int32)Converts the numeric value of each element of a specified subarray of bytes to its equivalent hexadecimal string representation.
ToUInt16(Byte[], Int32)Returns a 16-bit unsigned integer converted from two bytes at a specified position in a byte array.

Let us see some examples −

The BitConverter.ToBoolean() method in C# returns a Boolean value converted from the byte at a specified position in a byte array.

Syntax

Following is the syntax −

public static bool ToBoolean (byte[] arr, int startIndex);

Above, arr is a byte array, whereas startIndex is the index of the byte within a value.

Example

Let us now see an example to implement the BitConverter.ToBoolean() method −

 Live Demo

using System;
public class Demo {
   public static void Main(){
      byte[] arr = { 50, 100 };
      Console.WriteLine("Array values...");
      for (int i = 0; i < arr.Length; i++) {
         Console.WriteLine("{0} ", arr[i]);
      }
      Console.WriteLine("
Converted values...");       for (int index = 0; index < arr.Length; index++) {          bool res = BitConverter.ToBoolean(arr, index);          Console.WriteLine(""+res);       }    } }

Output

This will produce the following output −

Array values...
50
100
Converted values...
True
True

The BitConverter.DoubleToInt64Bits() method in C# is used to convert the specified double-precision floating-point number to a 64-bit signed integer.

Syntax

Following is the syntax −

public static long DoubleToInt64Bits (double val);

Above, val is the number to convert.

Example

Let us now see an example to implement the BitConverter.DoubleToInt64Bits() method −

 Live Demo

using System;
public class Demo {
   public static void Main(){
      double d = 5.646587687;
      Console.Write("Value = "+d);
      long res = BitConverter.DoubleToInt64Bits(d);
      Console.Write("
64-bit signed integer = "+res);    } }

Output

This will produce the following output −

Value = 5.646587687 
64-bit signed integer = 4618043510978159912

Updated on: 04-Dec-2019

872 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements