How to get the bits of a “double” as a “long”

后端 未结 3 1503
太阳男子
太阳男子 2021-02-20 04:58

I would like to manipulate the bitwise representation of floating-point numbers in C#. BinaryWriter and BinaryReader do it this way:

public virtual unsafe void W         


        
3条回答
  •  有刺的猬
    2021-02-20 05:26

    Are you trying to avoid unsafe code altogether, or do you just want an alternative to those specific methods on BinaryReader and BinaryWriter?

    You could use BitConverter.DoubleToInt64Bits and BitConverter.Int64BitsToDouble, which are designed to do exactly what you need, although I think they use the same unsafe conversion behind-the-scenes as the BinaryReader/BinaryWriter methods.

提交回复
热议问题