简体   繁体   English

将C#方法转换为Java

[英]Convert C# method to Java

I am looking to convert the following bit of C# code to Java. 我希望将以下C#代码转换为Java。 I having a hard time coming up with a equivalent. 我很难想出一个等效的方法。

Working C# Code: 工作的C#代码:

private ushort ConvertBytes(byte a, byte b, bool flip)
{
    byte[] buffer = new byte[] { a, b };
    if (!flip)
    {
        return BitConverter.ToUInt16(buffer, 0);
    }
    ushort num = BitConverter.ToUInt16(buffer, 0);
    //this.Weight = num;
    int xy = 0x3720;
    int num2 = 0x3720 - num;
    if (num2 > -1)
    {
        return Convert.ToUInt16(num2);
    }
    return 1;
}

Here is the Java Code that does not work. 这是无效的Java代码。 The Big challenge is the "BitConverter.ToInt16(buffer,0). How do i get the Java equal of the working C# method. 最大的挑战是“ BitConverter.ToInt16(buffer,0)。如何使Java与工作的C#方法相等。

Java Code that is Wrong: 错误的Java代码:

private short ConvertBytes(byte a, byte b, boolean flip){
    byte[] buffer = new byte[] { a, b };
    if (!flip){
        return  (short) ((a << 8) | (b & 0xFF));
    }
    short num = (short) ((a << 8) | (b & 0xFF));
    //this.Weight = num;
    int num2 = 0x3720 - num;
    if (num2 > -1){
        return (short)num2;
    }
    return 1;
}
private short ConvertBytes(byte a, byte b, boolean flip){

    ByteBuffer byteBuffer = ByteBuffer.allocate(2);
    byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    byteBuffer.put(a);
    byteBuffer.put(b);
    short num = byteBuffer.getShort(0);

    //this.Weight = num;
    int num2 = 0x3720 - num;
    if (num2 > -1){
        return (short)num2;
    }
    return 1;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM