[英]Converting correctly Byte array to long C#
I would like to convert a byte array (byte[]) to a long in C#. I have already been able to research this and this thread is NOT a duplicate.我想将一个字节数组 (byte[]) 转换为 C# 中的一个 long。我已经能够研究这个并且这个线程不是重复的。 Indeed, I need the conversion to be BigEndian which is done this way in Java:
事实上,我需要转换为 BigEndian,这是在 Java 中以这种方式完成的:
ByteBuffer buf = ByteBuffer.wrap(digest);
Long bufLong = buf.getLong(12); // int parameter = start index;
For exemple, here is a Java / C# convertion comparaison.例如,这是一个 Java / C# 转换比较。 Java:
Java:
public static void testLong()
{
byte[] testLong = new byte[]{
(byte) 0xac, (byte) 0xe0, (byte) 0x46, (byte) 0x0b, (byte) 0xff,
(byte) 0x30, (byte) 0xbf, (byte) 0xbf, (byte) 0x86, (byte) 0xc3,
(byte) 0xaf, (byte) 0xf4, (byte) 0x6b, (byte) 0xfe, (byte) 0xc2,
};
ByteBuffer byteBuffer = ByteBuffer.wrap(testLong);
for (int i = 0; i < (testLong.length - 8); i++) {
System.out.println("i[" + i + "]: " + byteBuffer.getLong(i));
}
}
C#: C#:
public static void TestLong()
{
byte[] testLong = new byte[]{
(byte) 0xac, (byte) 0xe0, (byte) 0x46, (byte) 0x0b, (byte) 0xff,
(byte) 0x30, (byte) 0xbf, (byte) 0xbf, (byte) 0x86, (byte) 0xc3,
(byte) 0xaf, (byte) 0xf4, (byte) 0x6b, (byte) 0xfe, (byte) 0xc2,
};
for (int i = 0; i < (testLong.Length - 8); i++)
{
Console.WriteLine($"I[{i}] : {BitConverter.ToInt64(testLong, i)}");
}
Array.Reverse(testLong);
Console.WriteLine("Reversing the array ...");
for (int i = 0; i < (testLong.Length - 8); i++)
{
Console.WriteLine($"I[{i}] : {BitConverter.ToInt64(testLong, i)}");
}
}
Java Output (Correct one): Java Output(正确一个):
i[0]: -5989710487062790209
i[1]: -2286126570181509242
i[2]: 5047408392239285955
i[3]: 864463253588591535
i[4]: -58335965835186188
i[5]: 3512736819901887595
i[6]: -4629833716884804610
C# Output (Incorrect): C# Output(错误):
I[0] : -4629928019949592404
I[1] : -8737054534917208352
I[2] : -4357584761552696506
I[3] : -5781629338509050101
I[4] : -815218024020758273
I[5] : 7779035710689886000
I[6] : -113728329830973505
Reversing the array ...
I[0] : -4645810805098676542
I[1] : -4629833716884804610
I[2] : 3512736819901887595
I[3] : -58335965835186188
I[4] : 864463253588591535
I[5] : 5047408392239285955
I[6] : -2286126570181509242
Note that i test to reverse the array because Java is BigEndian and C# is LittleEndian (Probably also the source of my problem but idk)请注意,我测试反转数组,因为 Java 是 BigEndian,而 C# 是 LittleEndian(可能也是我的问题的根源,但 idk)
As you can see the convertion is not good.如您所见,转换效果不佳。
I also tried to use a ByteBuffer port in C# without sucess: https://github.com/google/flatbuffers/blob/master.net/FlatBuffers/ByteBuffer.cs我还尝试在 C# 中使用 ByteBuffer 端口但没有成功: https://github.com/google/flatbuffers/blob/master.net/FlatBuffers/ByteBuffer.cs
If someone can help me with this problem, i would really appreciate it.如果有人可以帮助我解决这个问题,我将不胜感激。
C# is not any endian - in that unless you cheat and look under the covers: endianness is not a factor you see; C# 不是任何字节序 - 除非你作弊并查看幕后:字节序不是你看到的一个因素; .NET in general, and
BitConverter
in particular, are CPU-endian; .NET,特别是
BitConverter
,是 CPU-endian; and presumably your CPU is little-endian.并且大概您的 CPU 是小端。
So: don't use that!所以:不要使用它!
BinaryPrimitives
offers explicit-endian APIs; BinaryPrimitives
提供显式字节序 API; use the ones that are correct for your scenario.使用适合您的场景的那些。
Note: when reversing data for endianness reasons, you'd only reverse that segment , not the entire payload - which is why your Reverse
attempt failed.注意:当出于字节顺序原因反转数据时,您只会反转该段,而不是整个有效负载 - 这就是您的
Reverse
尝试失败的原因。
using System;
using System.Buffers.Binary;
ReadOnlySpan<byte> testLong = new byte[]{
0xac, 0xe0, 0x46, 0x0b, 0xff,
0x30, 0xbf, 0xbf, 0x86, 0xc3,
0xaf, 0xf4, 0x6b, 0xfe, 0xc2,
};
for (int i = 0; i < (testLong.Length - 8); i++)
{
Console.WriteLine($"I[{i}] : {BinaryPrimitives.ReadInt64BigEndian(testLong.Slice(i))}");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.