简体   繁体   English

Java-byte []到int的转换,反之亦然

[英]Java - byte[] to int conversion and vice versa

I am porting some php code as well as python code, which are two functions. 我正在移植一些php代码以及python代码,这是两个功能。 The purpose of them is parsing an integer to a byte array and vice versa. 它们的目的是将整数解析为字节数组,反之亦然。 However it worked for a while, but now I get invalid integer errors. 但是它工作了一段时间,但是现在我得到了无效的整数错误。

I got the following python snippet which I converted to Java: 我得到了以下Python代码段,这些代码段已转换为Java:

def _deAdjustId(val):
    if sys.version_info >= (3,0):
        valEnc = val.encode('latin-1') if type(val) is str else val
    else:
        valEnc = val
    return int(binascii.hexlify(valEnc), 16)

And my Java code: 而我的Java代码:

private static int deAdjustId(byte[] val) {
    String valEnc;
    try {
        valEnc = new String(val, "iso-8859-1");
    } catch (UnsupportedEncodingException e) {
        valEnc = new String(val);
    }
    return Integer.parseInt(BinAscii.hexlify(valEnc.getBytes()), 16);
}

The error given here is the following: 此处给出的错误如下:

caught exception: Invalid int: "325FC398C3AB"

Does this mean that the integer value is too big, or what is the problem? 这是否意味着整数值太大,或者是什么问题?

valEnc.getBytes() will convert the String to bytes using the host platform's default encoding, which is going to be either UTF-16LE (in Windows) or UTF-8 (all other platforms, including yours). valEnc.getBytes()将使用主机平台的默认编码将String转换为字节,该编码将为UTF-16LE(在Windows中)或UTF-8(所有其他平台,包括您的平台)。

Your String contains ISO 8859-1 characters, but any of those characters with values above 127 are being decoded as multiple bytes by valEnc.getBytes() . 您的字符串包含ISO 8859-1字符,但是valEnc.getBytes()那些值大于127的字符解码为多个字节。 To fix this, use valEnc.getBytes(StandardCharsets.ISO_8859_1) . 要解决此问题,请使用valEnc.getBytes(StandardCharsets.ISO_8859_1) Then you will be guaranteed to get one byte per character. 这样就可以保证每个字符获得一个字节。

However, that raises the question of why you would bother using String in the first place. 但是,这提出了一个问题,为什么您首先要打扰使用String。 You already have a byte array whose bytes each represent an ISO 8859-1 character. 您已经有一个字节数组,其每个字节代表一个ISO 8859-1字符。 You should remove valEnc from your method completely. 您应该从方法中完全删除valEnc

In fact, there is no reason to convert the bytes to hex and then parse them. 实际上,没有理由将字节转换为十六进制然后进行解析。 You can just convert four bytes to an int with this: 您可以使用以下命令将四个字节转换为一个int:

return ByteBuffer.wrap(val).getInt();

See the documentation for ByteBuffer for more information, particularly the getInt method. 有关更多信息,请参见ByteBuffer文档 ,尤其是getInt方法。

Update: It seems val has no guarantees regarding its size, so you'll want to account for that: 更新:似乎val无法保证其大小,因此您需要考虑以下因素:

if (val == null) {
    return 0;
}

if (val.length >= 4) {
    return ByteBuffer.wrap(val).getInt();
}

ByteBuffer buffer = ByteBuffer.allocate(4);
buffer.position(4 - val.length);
buffer.put(val);
buffer.rewind();
return buffer.getInt();

Python int is like BigInteger in Java: Python int类似于Java中的BigInteger

private static BigInteger deAdjustId(byte[] val) {
    String valEnc;
    try {
        valEnc = new String(val, "iso-8859-1");
    } catch (UnsupportedEncodingException e) {
        valEnc = new String(val);
    }
    return new BigInteger(BinAscii.hexlify(valEnc.getBytes()), 16);
}

or even: 甚至:

private static BigInteger deAdjustId(byte[] val) {
    return new BigInteger(val);
}

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

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