gpt4 book ai didi

c# - 将两条短裤放入字节数组

转载 作者:行者123 更新时间:2023-11-29 05:49:35 25 4
gpt4 key购买 nike

我需要一个包含 2 个短裤的 4 个字节的字节数组。我现在有这个:

MemoryStream str = new MemoryStream();
using (BinaryWriter writer = new BinaryWriter(stream)) {
writer.Write((short) printscreen.Width);
writer.Write((short) printscreen.Height);
}
byte[] bytes = str.ToArray();

在 java 中我使用了 bytebuffer。但是我应该为 C# 使用什么?

我通过套接字将这些字节发送到 Java 服务器。 Java代码:

byte[] data = new byte[in.available()];
in.read(data);
ByteBuffer buffer = ByteBuffer.wrap(data);
System.out.println(buffer.getShort());

最佳答案

来自 Java 的文档 ByteBuffer Class :

Primitive values are translated to (or from) sequences of bytes according to the buffer's current byte order, which may be retrieved and modified via the order methods. Specific byte orders are represented by instances of the ByteOrder class. The initial order of a byte buffer is always BIG_ENDIAN.

来自 .NET 的文档 BinaryWriter.Write(short) Method :

BinaryWriter stores this data type in little endian format.

由于您无法更改 BinaryWriter,因此您需要更改 ByteBuffer 使用的字节顺序:

buffer.order(ByteOrder.LITTLE_ENDIAN);

关于c# - 将两条短裤放入字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14445992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com