java中bitconverter_Java Double.doubleToLongBits到C#BitConverter.ToDouble(Java Double.doubleToLongBits ...

Java Double.doubleToLongBits到C#BitConverter.ToDouble(Java Double.doubleToLongBits into C# BitConverter.ToDouble)

鉴于C#docs没有提及IEEE 754,但确实谈到了字节顺序,表明平台依赖性,我怀疑不是一般情况。 如果我知道Java和C#进程都在Intel X64架构上运行怎么办? 在Linux上使用Java,在Windows上使用C#。 任何细节都会很有趣。

Can I convert a Java double to 8 bytes using java.lang.Double.doubleToLongBits, write to network, then read those bits in C# using BitConverter.ToDouble?

Given the C# docs make no mention of IEEE 754 but do talk about endianness, indicating platform dependence, I suspect not in the general case. What if I know both Java and C# processes are running on Intel X64 architecture? With Java on Linux, C# on Windows. Any details would be interesting.

原文:https://stackoverflow.com/questions/42739998

2020-01-26 18:01

满意答案

应该可以,C#确实使用IEEE 754双打。 唯一的问题可能是通过网络传输时的字节序,所以只需选择您的架构正在使用的那个,它应该正确传输。

注意,如果使用ByteBuffer ,则可以轻松选择所需的字节序。 只需使用它的double函数,不需要使用doubleToLongBits - 这就是你需要将双重表示转换为long的情况。

It should be possible, C# does use IEEE 754 doubles. The only problem might be the endianness when transfering them over the network, so just choose the one your architecture is using and it should transfer correctly.

Note, if you use a ByteBuffer, you can select the desired endian easily. Just use its double functions, no need to use doubleToLongBits -- that's just there in case you need the double representation to be converted to a long.

2017-03-11

相关问答

@ Shoover的回答是正确的,但有一点比这更多。 作为Double::equals的javadoc状态: “这个定义允许散列表正确运行。” 假设Java设计者决定在包装的double实例上实现equals(...)和compare(...)与==相同的语义。 这将意味着equals()将始终为包装的NaN返回false 。 现在考虑如果您尝试在地图或集合中使用包装的NaN会发生什么。 List l = new ArrayList();

l.add(Double....

这只是endianness(-46和210是因为Java的签名字节,但这只是一个UI的事情)。 要么颠倒数组内容,要么使用移位操作来写入int。 注意:.NET发出的排序依赖于平台。 我建议在两种情况下都使用KNOWN ENDIANNESS; 最有可能的是通过使用两者的移位操作。 或者更好的主意:只需使用预先封装的,独立于平台的序列化格式(例如:对Java和.NET / C#都具有良好支持的协议缓冲区)。 例如; 如果我正在向byte[] buffer写入一个int value (从offset开...

有Math.nextUp方法 。 返回在正无穷大方向上与d相邻的浮点值。 有重载需要double和float 。 还有相应的Math.nextDown方法。 There is the Math.nextUp method. Returns the floating-point value adjacent to d in the direction of positive infinity. There are overloads to take double and float. There a...

如评论中所述,尝试使用ByteBuffer.order(ByteOrder)方法在ByteBuffer.order(ByteOrder)上翻转字节顺序 As mentioned in the comments, try flipping the byte order on the ByteBuffer using the ByteBuffer.order(ByteOrder) method

目标C中没有安全的方法将double的位分配给long 。在Java中, long和double都是64位。 在某些情况下,对于Objective C, long为32位, double为64位。 你应该用long long代替。 int intValue = *((int*)(&floatValue));

long long llValue = *((long long*)(&doubleValue));

There's no safe way to assign the bits of a ...

你不能在C#中拥有最终参数 方法默认为“final”。 C#中没有未签名的转移权限 所以我们得到: protected static int putLong(byte [] b, int off, long val) {

b[off + 7] = (byte) (val >> 0);

b[off + 6] = (byte) (val >> 8);

b[off + 5] = (byte) (val >> 16);

b[off + 4] = (byte) (val >>...

这比你想象的容易; 其全部关于缩放(当从0-1范围到其他范围时也是如此)。 基本上,如果你知道你有64个真正的随机位(8字节),那么只需要这样做: double zeroToOneDouble = (double)(BitConverter.ToUInt64(bytes) / (decimal)ulong.MaxValue);

这种算法的麻烦来自于你的“随机”位实际上并不是一般的随机数。 那时你需要一个专门的算法,比如Mersenne Twister 。 This is easier than ...

标志的0是“缺失”只是一个数字写作惯例。 在十进制系统中,你会写1067而不是0001067,对吧? 这只是Java所做的。 内存中存在0,它们只是不显示,因为你不需要它们。 这也是二进制数的浮点表示。 如果你将1.5 * 10 ^ -12转换为二进制数,它在开始时将有很多0(我开始了,但随后我的论文结束了 - 开头肯定有超过12个)。 然后对该二进制数进行归一化(使得在该点之前只有一个1),并且将该归一化的指数用作指数。 我猜二进制指数1.5 * 10 ^ -12确实是40。 换句话说:IEEE...

您是否意识到您的二进制数据是OLE自动化日期值的二进制抑制? 所以不要long ,你应该从你的数组中获得一个double值。 var b = new byte[8];

b[0] = 0x20;

b[1] = 0x64;

b[2] = 0xa8;

b[3] = 0xac;

b[4] = 0xb6;

b[5] = 0x65;

b[6] = 0xe4;

b[7] = 0x40;

var dbl = BitConverter.ToDouble(b, 0);

var dt = DateTime.FromOA...

应该可以,C#确实使用IEEE 754双打。 唯一的问题可能是通过网络传输时的字节序,所以只需选择您的架构正在使用的那个,它应该正确传输。 注意,如果使用ByteBuffer ,则可以轻松选择所需的字节序。 只需使用它的double函数,不需要使用doubleToLongBits - 这就是你需要将双重表示转换为long的情况。 It should be possible, C# does use IEEE 754 doubles. The only problem might be the en...

相关文章

java的double类型要保留两位小数有四种方法,都是四舍五入,例: import java.mat

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值