C# 用于Java的位转换器

C# 用于Java的位转换器,c#,java,bytearray,primitive,C#,Java,Bytearray,Primitive,按照问题中提供的建议,我已经开始为Java实现我自己的bitconverter,但没有得到相同的结果 有人能告诉我我可能做得不对吗 public static byte[] GetBytes(Integer value) { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); DataOutputStream stream = new DataOutputStream(byteStream); t

按照问题中提供的建议,我已经开始为Java实现我自己的bitconverter,但没有得到相同的结果

有人能告诉我我可能做得不对吗

public static byte[] GetBytes(Integer value) {
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    DataOutputStream stream = new DataOutputStream(byteStream);
    try {
        stream.writeInt(value);
    } catch (IOException e) {
        return new byte[4];
    }
    return byteStream.toByteArray();
}

byte[] result = BitConverter.GetBytes(1234); //JAVA: [0, 0, 4, -46]
byte[] result = BitConverter.GetBytes(1234); //C#: [210, 4, 0, 0]
这就是endianness(-46和210是因为Java有符号字节,但这只是一个UI)。反转数组内容,或使用shift操作写入int

注意:.NET发出的终止性取决于平台。我建议在这两种情况下都使用已知的端点;最有可能的方法是使用两者的轮班操作。或者更好的主意是:只需使用预先封装的、独立于平台的序列化格式(例如:protocol buffers,它在Java和.NET/C上都有很好的支持)

比如,;如果我正在将
int值
写入
字节[]缓冲区
(从
偏移量开始),我可能会使用:

buffer[offset++] = (byte)value;
buffer[offset++] = (byte)(value>>8);
buffer[offset++] = (byte)(value>>16);
buffer[offset++] = (byte)(value>>24);
这保证了小端,类似的代码应该可以在任何框架上工作。

C#
位转换器将使用底层架构的端。在大多数环境中,它将是little endian(就像您的情况一样)。然而,Java的
DataOutputStream
总是以big-endian(“可移植方式”)编写。如果您想匹配该行为,您需要检查机器的端点并相应地写入

而且,java中的字节是有符号的,因此输出只是一个装饰性的差异。位表示是相同的,因此您不必担心这一点

要检查机器的端部,请使用以下方法。然后使用指定字节并写入数据的

然后,您可以像这样实现您的方法:

public static byte[] GetBytes(int value)
{
    ByteBuffer buffer = ByteBuffer.allocate(4).order(ByteOrder.nativeOrder());
    buffer.putInt(value);
    return buffer.array();
}

基于Jeff的答案,您可以使用一个
字节缓冲符
来转换
int
字节[]
。下面是您可以放入类中以转换为Little Endian或从Little Endian转换为Little Endian的代码:

ByteBuffer _intShifter = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE)
                                   .order(ByteOrder.LITTLE_ENDIAN);

public byte[] intToByte(int value) {
    _intShifter.clear();
    _intShifter.putInt(value);      
    return _intShifter.array();
}

public int byteToInt(byte[] data)
{
    _intShifter.clear();
    _intShifter.put(data, 0, Integer.SIZE / Byte.SIZE);
    _intShifter.flip();
    return _intShifter.getInt();
}
如果任何主体需要..C#到JAVA BitConverter.ToInt32

  public static int toInt32_2(byte[] bytes, int index)
        {
            int a = (int)((int)(0xff & bytes[index]) << 32 | (int)(0xff & bytes[index + 1]) << 40 | (int)(0xff & bytes[index + 2]) << 48 | (int)(0xff & bytes[index + 3]) << 56);
            // int a = (int)((int)(0xff & bytes[index]) << 56 | (int)(0xff & bytes[index + 1]) << 48 | (int)(0xff & bytes[index + 2]) << 40 | (int)(0xff & bytes[index + 3]) << 32);
            //Array.Resize;
            return a;
        }

什么是偏移值?
    public static short toInt16(byte[] bytes, int index) //throws Exception
    {
        return (short)((bytes[index + 1] & 0xFF) | ((bytes[index] & 0xFF) << 0));
        //return (short)(
        //        (0xff & bytes[index]) << 8 |
        //                (0xff & bytes[index + 1]) << 0
        //);
    }
public static byte[] GetBytesU16(long value)
{

    ByteBuffer buffer = ByteBuffer.allocate(8).order(ByteOrder.nativeOrder());
    buffer.putLong(value);
    return buffer.array();
}