Open In App

C# | BitConverter Class

Improve
Improve
Like Article
Like
Save
Share
Report

The use of BitConverter Class is to convert a base data types to an array of bytes and an array of bytes to base data types. This class is defined under System namespace. This class provides different types of methods to perform the conversion. Basically, a byte is defined as an 8-bit unsigned integer. This class helps in manipulating the value types in their fundamental form, as a series of bytes. It is done with the help of static methods. The static methods of BitConverter class are used to convert each of the primitive types to and from an array of bytes.

Field:

  • IsLittleEndian: This indicates the byte order (“endianness”) in which data is stored in this computer architecture.

Methods

Method Description
DoubleToInt64Bits(Double) Converts the specified double-precision floating point number to a 64-bit signed integer.
GetBytes() Converts the specified data to an array of bytes.
Int32BitsToSingle() Converts the specified 32-bit signed integer to a single-precision floating point number.
Int64BitsToDouble(Int64) Converts the specified 64-bit signed integer to a double-precision floating point number.
SingleToInt32Bits() Converts the specified single-precision floating point number to 32-bit signed integer.
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.
ToDouble(Byte[], Int32) Returns a double-precision floating point number converted from eight bytes at a specified position in a byte array.
ToInt16(Byte[], Int32) Returns a 16-bit signed integer converted from two bytes at a specified position in a byte array.
ToInt32(Byte[], Int32) Returns a 32-bit signed integer converted from four bytes at a specified position in a byte array.
ToInt64(Byte[], Int32) Returns a 64-bit signed integer converted from eight bytes at a specified position in a byte array.
ToSingle(Byte[], Int32) Returns a single-precision floating point number converted from four bytes at a specified position in a byte array.
ToString() Converts the numeric value of each element of a specified array 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.
ToUInt32(Byte[], Int32) Returns a 32-bit unsigned integer converted from four bytes at a specified position in a byte array.
ToUInt64(Byte[], Int32) Returns a 64-bit unsigned integer converted from eight bytes at a specified position in a byte array.

Example 1:




// C# program to illustrate the
// use of GetBytes(Int64) method
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating and initializing an array
        long[] ele = {0, long.MaxValue, long.MinValue,
                         1000000000000000, -100000000, 
                       0xDDDDDDDDD, -0xAAAAAAAAAAAA };
  
        // Display the elements
        Console.WriteLine("Elements are:");
        foreach(long i in ele)
        {
            Console.WriteLine(i);
        }
  
        foreach(var num in ele)
        {
  
            // Get the specified 64-bit signed
            // integer value as an array of bytes.
            // using GetBytes(Int64) method
            byte[] BArr = BitConverter.GetBytes(num);
  
            // Display the elements
            Console.WriteLine("Element: {0}"
                BitConverter.ToString(BArr));
        }
    }
}


Output:

Elements are:
0
9223372036854775807
-9223372036854775808
1000000000000000
-100000000
59556879837
-187649984473770
Element: 00-00-00-00-00-00-00-00
Element: FF-FF-FF-FF-FF-FF-FF-7F
Element: 00-00-00-00-00-00-00-80
Element: 00-80-C6-A4-7E-8D-03-00
Element: 00-1F-0A-FA-FF-FF-FF-FF
Element: DD-DD-DD-DD-0D-00-00-00
Element: 56-55-55-55-55-55-FF-FF

Example 2:




// C# program to illustrate the
// use of Int64BitsToDouble(Int64)
// method
using System;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        long ele = 0xFFFFFFFFFFFFFFF;
  
        // Converts the given 64-bit signed
        // integer to a double-precision 
        // floating point number using the 
        // Int64BitsToDouble(Int64) method
        double R = BitConverter.Int64BitsToDouble(ele);
  
        // Display the element
        Console.WriteLine("Converted Element is : {0}", R);
    }
}


Output:

Converted Element is : 1.28822975391943E-231

Reference:



Last Updated : 20 Feb, 2019
Like Article
Save Article
Share your thoughts in the comments
Similar Reads