字节数组到java中的unsigned int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1576374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:25:05  来源:igfitidea点击:

byte array to unsigned int in java

java

提问by venu

I'm converting a byte array into intby doing this:

我正在int通过执行以下操作将字节数组转换为:

ByteArrayInputStream bais = new ByteArrayInputStream (data);
DataInputStream dis = new DataInputStream (bais);
int j = dis.readInt();

But it returns a signed number where I want an unsigned number, because I want to send that number to the server as an integer during encryption and have to decrypt it at server. if it is a signed number i can do that.

但它返回一个有符号数字,我想要一个无符号数字,因为我想在加密期间将该数字作为整数发送到服务器,并且必须在服务器上对其进行解密。如果它是一个签名号码,我可以做到。

please any 1 help me..........

请任何1帮助我......

venu

维努

回答by aberrant80

Java does not have unsigned int, so you'll have to manually invert the bits to get an unsigned value if you absolutely need to have one.

Java 没有 unsigned int,因此如果您绝对需要一个无符号值,则必须手动反转位以获得无符号值。

Got this from Google.

从谷歌得到这个

回答by erickson

An intis always a signed, 32-bit number in Java. However, this only matters if you are doing math with it. If all you care about is the pattern of 0 and 1 bits, simply ignore the sign.

Anint在 Java 中始终是有符号的 32 位数字。但是,这仅在您使用它进行数学运算时才重要。如果您只关心 0 和 1 位的模式,只需忽略符号即可。

If you do need to do some math, convert it to a longby masking:

如果您确实需要做一些数学运算,请long通过屏蔽将其转换为 a :

long l = j & 0xFFFFFFFFL;

Do all arithmetic with longoperands, modulo 0xFFFFFFFFL. When you are done, cast the result back to an intand transmit it.

long操作数进行所有算术运算,取模 0xFFFFFFFFFL。完成后,将结果转换回 anint并传输它。

回答by Stephen C

Java's primitive integer types (i.e. byte, short, int and long) are all signed. But you can work around this.

Java 的原始整数类型(即 byte、short、int 和 long)都是有符号的。但是你可以解决这个问题。

To do (say) 32 bit unsigned arithmetic, just do the arithmetic pretending that 'int' is unsigned. For example, 2**31 - 1is the largest (signed) intvalue. If you add one to it you will get -2**31. But that bit pattern is the same as +2**31if you think of the intas being unsigned. This also works for subtraction and multiplication. (I'm not sure about division and remainder, but the chances are that doesn't matter for you).

要执行(例如)32 位无符号算术,只需假装“int”是无符号的算术即可。例如,2**31 - 1是最大(有符号)int值。如果你添加一个,你会得到-2**31。但是该位模式与+2**31您将int视为无符号的情况相同。这也适用于减法和乘法。(我不确定除法和余数,但很有可能这对你来说无关紧要)。

Comparing unsigned 32 bit values is a bit more tricky. For example, -1is less that +1, but if you interpret -1as an unsigned value you get +2**32 - 1which should be greater than '+1'. You can compensate by translating the inequality (I'll leave it to the reader to figure it out) or by casting the intvalues to long, masking them with 0xffffffffLand comparing them as longs; e.g.

比较无符号的 32 位值有点棘手。例如,-1小于+1,但如果您将其解释-1为无符号值,您会得到+2**32 - 1应该大于“+1”的值。您可以通过翻译不等式(我将其留给读者自己弄清楚)或将int值转换为long、用 掩蔽它们0xffffffffL并将它们作为 long 进行比较来进行补偿;例如

int u1 = ...
int u2 = ...
if ((((long) u1) & 0xffffffff) < (((long) u2) & 0xffffffff) {
    // u1 represents a smaller unsigned value than u2
}

Converting 32 bit unsigned integers to Strings is easiest done using longs; e.g.

使用 longs 最容易将 32 位无符号整数转换为字符串;例如

String str = Long.toString(((long) u1) & 0xffffffffL);

Now I'll freely admit that using intto represent 32 bit unsigned values is tricky, and potentially error prone. A cleaner solution would be to use longthroughout, or if your application needs 64 bit unsigned values to use BigInteger.

现在我会坦率地承认,使用int表示 32 位无符号值很棘手,而且可能容易出错。更简洁的解决方案是long始终使用,或者如果您的应用程序需要 64 位无符号值才能使用BigInteger.



UPDATE- it looks Java 8 will have support (in the form of library methods) for treating intand longas unsigned types - see "Unsigned Integer Arithmetic API now in JDK 8"by Joseph Darcy @ Oracle.

更新- 看起来 Java 8 将支持(以库方法的形式)处理intlong作为无符号类型 - 请参阅Joseph Darcy @ Oracle 的“JDK 8 中的无符号整数算术 API”

回答by Asaf Pinhassi

Each cell in the array is treated as unsigned int:

数组中的每个单元格都被视为无符号整数:

private int unsignedIntFromByteArray(byte[] bytes) {
    int res = 0;
    if (bytes == null)
        return res;

    for (int i = 0; i < bytes.length; i++) {
        res = (res *10) + ((bytes[i] & 0xff));
    }
    return res;
}
  • The code will convert [12,34] to 1234
  • 代码会将 [12,34] 转换为 1234