在C#中的等价代码为:“`BitConverter.ToInt32“`。

huangapple go评论85阅读模式
英文:

ByteBuffer.wrap().getInt() equivalent in c#

问题

**Java**

byte[] input = new byte[] { 83, 77, 45, 71, 57, 51, 53, 70 };

int buff = ByteBuffer.wrap(input).getInt();

输出:`1397566791`

**C**

byte[] array = { 83, 77, 45, 71, 57, 51, 53, 70 };

MemoryStream stream = new MemoryStream();
using (BinaryWriter writer = new BinaryWriter(stream))
{
    writer.Write(array);
}
byte[] bytes = stream.ToArray();

int buff = BitConverter.ToInt32(bytes, 0);

输出:`1194151251`

我不知道如何获得*相同的*输出

谢谢
英文:

Java

byte[] input = new byte[] { 83, 77, 45, 71, 57, 51, 53, 70 };

int buff = ByteBuffer.wrap(input).getInt();

Output: 1397566791

C#

byte [] array = { 83, 77, 45, 71, 57, 51, 53, 70 };

MemoryStream stream = new MemoryStream();
using (BinaryWriter writer = new BinaryWriter(stream))
{
     writer.Write(array);
}
byte[] bytes = stream.ToArray();

int buff = BitConverter.ToInt32(bytes, 0);

Output: 1194151251

I have no idea how to get the same output

Thanks

答案1

得分: 2

好的,以下是翻译好的部分:

好的,`Int32` 仅包含 `4` 个字节,让我们通过 `Take(4)` 来获取它们的帮助。接下来,我们必须考虑 *结束*(Big 或 Little)并在必要时`Reverse`这些 `4` 个字节:

      using System.Linq;

      ... 

      byte[] array = { 83, 77, 45, 71, 57, 51, 53, 70 };
            
      // 1397566791
      int buff = BitConverter.ToInt32(BitConverter.IsLittleEndian 
        ? array.Take(4).Reverse().ToArray()
        : array.Take(4).ToArray());
英文:

Well, Int32 consists of 4 bytes only, let's Take them with the help of Take(4). Next, we have to take ending (Big or Little) into account and Reverse these 4 bytes if necessary:

  using System.Linq;

  ... 

  byte[] array = { 83, 77, 45, 71, 57, 51, 53, 70 };
        
  // 1397566791
  int buff = BitConverter.ToInt32(BitConverter.IsLittleEndian 
    ? array.Take(4).Reverse().ToArray()
    : array.Take(4).ToArray());

答案2

得分: 1

在Java情况下,它会按顺序取前4个字节并将它们转换为int。

System.out.println((((((83<<8)+77)<<8)+45)<<8)+71);
1397566791

在C#中,它会按相反的顺序取前四个字节。

System.out.println((((((71<<8)+45)<<8)+77)<<8)+83);
1194151251

因此,您需要阅读描述两个类操作的API文档,并相应地使用它们。应该有一种方法可以反转字节顺序。

从C#转到Java,可能会像这样。

buff = ByteBuffer.wrap(input).order(ByteOrder.LITTLE_ENDIAN).getInt();
System.out.println(buff);

输出

1194151251
英文:

In the Java case, it's taking the first 4 bytes in order and converting them to an int.

System.out.println((((((83&lt;&lt;8)+77)&lt;&lt;8)+45)&lt;&lt;8)+71);
1397566791

In C# it is taking the first four in reverse order.

System.out.println((((((71&lt;&lt;8)+45)&lt;&lt;8)+77)&lt;&lt;8)+83);
1194151251

So you need to read the API documentation that describes the operation for both classes and use them accordingly. There should be a way to reverse the byte order.

To go from C# to Java it would be something like this.

buff = ByteBuffer.wrap(input).order(ByteOrder.LITTLE_ENDIAN).getInt();
System.out.println(buff);

Prints

1194151251



</details>



huangapple
  • 本文由 发表于 2020年9月14日 02:26:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63874175.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定