Java和C#-字节数组到长转换的区别 [英] Java and C# - byte array to long conversion difference
本文介绍了Java和C#-字节数组到长转换的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这对我来说很奇怪: 当我在Java中运行时
This is strange to me: when I run in Java
byte[] data = new byte[] { 50, -106, 40, -22, -94, -119, -52, 8 };
ByteBuffer bb = ByteBuffer.wrap( data );
System.out.println( bb.getLong() );
结果是3645145936617393160
result is 3645145936617393160
当我在C#中运行
//unsigned values (signed&0xff)
byte[] bytes = new byte[] { 50, 150, 40, 234, 162, 137, 204, 8 };
long l = BitConverter.ToInt64(bytes, 0);
System.Console.Write(String.Format("{0}\n", l));
System.Console.ReadKey();
结果是634032980358633010
result is 634032980358633010
您能帮我理解吗?
谢谢!
Can you help me to understand this?
Thanks!
推荐答案
这与 endianness .
如果反转字节数组,它将按预期工作:
If you reverse the byte array, it works as expected:
BitConverter.ToInt64(new byte[] { 8, 204, 137, 162, 234, 40, 150, 50 }, 0)
You can set the endianness in Java by calling bb.order(ByteOrder.LITTLE_ENDIAN)
.
顺便说一句,最简单的方法是使用 LINQPad .
By the way, the easiest way to play with these things is to use LINQPad.
这篇关于Java和C#-字节数组到长转换的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文