将 C# 代码迁移到 Java、无符号短整型和字节数组转换
我正在用 Java 编写一段代码(我对 Java 相当陌生),我之前用 C# 编写过。这是 C# 中的代码和示例。
ushort number = 0xAABB; // 43707
byte[] data = new byte[2];
EndianBitConverter.Big.CopyBytes(number, data, 0); // value[0] = 170, value[1] = 187
我在 .NET 中使用自定义位转换器,因为它默认为小端。无论如何,根据我对java的理解,如果我想使用与 byte[] 相同的结果,我应该期望我的值(170和187)小128(Byte.MAX_VALUE + 1),即(42, 59) - 由于 .net 和 java 对于字节类型有不同的范围。 这是我用 Java 编写的内容来模仿我的上述逻辑。
public class Ushort {
private int value = 0;
public Ushort(int i) {
value = i - (Short.MAX_VALUE + 1);
}
public int get() {
return value;
}
public byte[] getBytes() {
byte[] result = new byte[]{
(byte) (value >>> 24),
(byte) (value >>> 16),
(byte) (value >>> 8),
(byte) value};
return new byte[]{result[2], result[3]};
}
}
但是,当我调用上面的代码时,
new Ushort(0xAABB).getBytes()
结果是 [42, -69] 而不是 [42, 59]。最后一个字节比应有的字节小 128。 我真的需要一些关于如何正确执行此操作以及我的逻辑是否正确的指导。 我还需要对 uint、ulong 等做同样的事情,所以我需要正确理解这一点。
I am writing a piece of code in Java (I'm fairly new to Java) that I have previously written in C#. Here's the code and the example in C#.
ushort number = 0xAABB; // 43707
byte[] data = new byte[2];
EndianBitConverter.Big.CopyBytes(number, data, 0); // value[0] = 170, value[1] = 187
I'm using custom bit convrter in .NET since it defaults to little endian. Anyway, from what I understand about java, if I want to use the same result as byte[] I should expect my values (170 and 187) to be smaller by 128 (Byte.MAX_VALUE + 1) that is (42, 59) - due to .net and java having different range for type byte.
Here's what I wrote in Java to mimic my above logic.
public class Ushort {
private int value = 0;
public Ushort(int i) {
value = i - (Short.MAX_VALUE + 1);
}
public int get() {
return value;
}
public byte[] getBytes() {
byte[] result = new byte[]{
(byte) (value >>> 24),
(byte) (value >>> 16),
(byte) (value >>> 8),
(byte) value};
return new byte[]{result[2], result[3]};
}
}
However when I call the above code with
new Ushort(0xAABB).getBytes()
The result is [42, -69] instead [42, 59]. The last byte is smaller by 128 than it should.
I really need some pointers on how to do this properly and if my logic is correct.
I also need to do the same for uint, ulong and so on, so I need to understand this correctly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要么我不明白你试图做的转换背后的原因,要么它们是错误的构思,这意味着我无法评论它们的实现是否存在错误。
java 中的 byte 类型与 C# 中的 sbyte 类型完全相同,因此您可以使用 sbyte 在 C# 中进行所有测试并进行 make在移植到 java 之前确保一切正常。
所以,在 Java 中你的字节数组应该是 { -86, -69 }。
Either I do not understand the reasons behind the conversions you are trying to do, or they are wrongly conceived, which means that I cannot opine as to whether there is an error in their implementation.
The type
byte
in java is the exact same as the typesbyte
in C#, so you can do all your testing in C# usingsbyte
and make sure things work correctly there before porting to java.So, in Java your byte array should be { -86, -69 }.
我没有测试它,但我会做的是:
I didn't test it, but what I would do is this: