/ConvertToDouble

Convert to double from byte array. The fonction allows convert byte array to double.

Primary LanguageC#

Converter MyBitToInt64

C#6.0 .NET Framework 4.6

This function allow You convert byte array to double


Purpose

The destination of this function convertation from byte array to double type. Assume You have byte array of some length and You want save that number in double type. For this destination You can use this project.


Converter class implementation

public class Converter
{
    static public double MyBitToInt64(byte[] arr)
    {
      StringBuilder strbuild = new StringBuilder();
      for (int i = 0; i < 4; i++)
      {
        strbuild.Append(arr[i]);
      }
      return Convert.ToInt64(strbuild.ToString());
    }  
}

Description of Converter class

The converter class has one static function called MyBitToInt64, which takes the array of byte and return the double. MyBitToInt64 represents the array as a line of numbers and return those numbers as a double.

Test

We invoke MyBitToInt64 method, which takes the byte array generated by RNGCryptoServiceProvider and return the double.

static void Main(string[] args)
{
      byte[] m = new byte[4];
      RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
      crypto.GetBytes(m);
      double exampl = Converter.MyBitToInt64(m);
      Console.WriteLine($"The double is: {exampl}");
      Console.ReadKey();
}

Result

MyBitToInt64 return double. The double is: 31283340