问题描述
我想操纵C#中浮点数的按位表示。 BinaryWriter和BinaryReader这样做:
pre $ public $ unsafe void void Write(double value)
ulong num = *((ulong *)& value);
...
public virtual unsafe double ReadDouble()
{
...
ulong num3 = ...;
return *((double *)& num3);
$ b
有没有办法做到这一点不安全的代码,没有开销实际上使用BinaryWriter和BinaryReader?
你试图完全避免不安全的代码,或者你只是想要替代那些特定的方法在 BinaryReader
和 BinaryWriter
?
和,它们被设计成完全符合你的需求,尽管我认为它们在后台使用和 BinaryReader $ c $一样的不安全转换c> /
BinaryWriter
方法。
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 Write(double value)
{
ulong num = *((ulong*) &value);
...
}
public virtual unsafe double ReadDouble()
{
...
ulong num3 = ...;
return *((double*) &num3);
}
Is there a way to do this without unsafe code, and without the overhead of actually using BinaryWriter and BinaryReader?
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.
这篇关于如何获得“双”的位作为“长”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!