我正在尝试将一些使用 BitConverter 的现有 C# 代码移植到 Java。我发现了各种其他线程,但后来发生在一个似乎可以解决问题的 github 类上。但是,ToUInt16 与我的 C# 代码的输出不匹配。ToInt16 和 ToInt32 似乎返回相同的值。你能帮我理解这个实现有什么问题(或者我做错了什么)吗?
代码参考:Java BitConverter
ToUInt16:
public static int ToUInt16(byte[] bytes, int offset) {
int result = (int)bytes[offset+1]&0xff;
result |= ((int)bytes[offset]&0xff) << 8;
if(Lysis.bDebug)
System.out.println(result & 0xffff);
return result & 0xffff;
}
ToUInt32:
public static long ToUInt32(byte[] bytes, int offset) {
long result = (int)bytes[offset]&0xff;
result |= ((int)bytes[offset+1]&0xff) << 8;
result |= ((int)bytes[offset+2]&0xff) << 16;
result |= ((int)bytes[offset+3]&0xff) << 24;
if(Lysis.bDebug)
System.out.println(result & 0xFFFFFFFFL);
return result & 0xFFFFFFFFL;
}
我的代码片段:
byte[] byteArray = from some byte array
int offset = currentOffset
int msTime = BitConverter.ToUInt16(byteArray, offset)
msTime 与来自 C# 的内容不匹配
C# 示例(来自供应商的字符串使用 Convert.FromBase64String 从字符串转换)
byte[] rawData = Convert.FromBase64String(vendorRawData);
byte[] sampleDataRaw = rawData;
Assert.AreEqual(15616, sampleDataRaw.Length);
//Show byte data for msTime
Console.WriteLine(sampleDataRaw[7]);
Console.WriteLine(sampleDataRaw[6]);
//Show byte data for msTime
Console.WriteLine(sampleDataRaw[14]);
Console.WriteLine(sampleDataRaw[13]);
var msTime = BitConverter.ToUInt16(sampleDataRaw, 6);
var dmWindow = BitConverter.ToUInt16(sampleDataRaw, 13);
Assert.AreEqual(399, msTime);
Assert.AreEqual(10, dmWindow);
字节值的 C# 控制台输出:
1
143
0
10
Groovy 示例(来自供应商的字符串使用 groovy decodeBase64() 从字符串转换)
def rawData = vendorRawData.decodeBase64()
def sampleDataRaw = rawData
Assert.assertEquals(15616, rawData.length)
//Show byte data for msTime
println sampleDataRaw[7]
println sampleDataRaw[6]
//Show byte data for dmWindow
println sampleDataRaw[14]
println sampleDataRaw[13]
def msTime = ToUInt16(sampleDataRaw, 6)
def dmWindow = ToUInt16(sampleDataRaw, 13)
Assert.assertEquals(399, msTime)
Assert.assertEquals(10, dmWindow)
**Asserts fail with**
399 fro msTime is actually 36609
10 from dmWindow is actually 2560
println中字节值的Groovy输出
1
-113
0
10