Convert Hexadecimal to Double

how do you set specific bits for double?

For int, I would do something like this:

public static int Value { get { return 0xfff8; } } 

What to do for a double?

 public static double Value { get { return 0xfff8; } } 

I'm a little worried that I can get an implicit conversion from an int 0xfff8 representation to a double floating point representation. However, I really want this 0xfff8 template to be type-independent.

+6
source share
2 answers

Check out the BitConverter class or go unsafe .

Unsafe example (untested):

 public unsafe double FromLong(long x) { return *((double*)&x); } 

BitConverter Example:

 double d = BitConverter.Int64BitsToDouble(0xdeadbeef); 

http://msdn2.microsoft.com/en-us/library/system.bitconverter.int64bitstodouble

+5
source
 byte bTemp = Convert.ToByte(hexValueInString, 16); double doubleTemp = Convert.ToDouble(bTemp); 

I am using .NET 4.0

+2
source

Source: https://habr.com/ru/post/896422/


All Articles