java中的字节数组为unsigned int [英] byte array to unsigned int in java

查看:136
本文介绍了java中的字节数组为unsigned int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过这样做将一个字节数组转换为 int

I'm converting a byte array into int by doing this:

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.

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

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

venu

推荐答案

Java的原始整数类型(即byte,short,int和long)都已签名。但你可以解决这个问题。

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

要做(比方说)32位无符号算术,只需要算术假装'int'是无符号的。例如, 2 ** 31 - 1 是最大(签名) int 值。如果您添加一个,您将获得 -2 ** 31 。但是,如果您认为 int 是无符号的,那么该位模式与 + 2 ** 31 相同。这也适用于减法和乘法。 (我不确定除法和余数,但是对你来说无关紧要)。

To do (say) 32 bit unsigned arithmetic, just do the arithmetic pretending that 'int' is unsigned. For example, 2**31 - 1 is the largest (signed) int value. If you add one to it you will get -2**31. But that bit pattern is the same as +2**31 if you think of the int as 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位值有点棘手。例如, -1 小于 +1 ,但如果您解释 -1 作为无符号值,你得到 + 2 ** 32 - 1 ,它应该大于'+1'。您可以通过翻译不平等来弥补(我会将其留给读者来解决)或者将 int 值转换为 long ,用 0xffffffffL 掩盖它们并将它们作为长数进行比较;例如

Comparing unsigned 32 bit values is a bit more tricky. For example, -1 is less that +1, but if you interpret -1 as an unsigned value you get +2**32 - 1 which 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 int values to long, masking them with 0xffffffffL and comparing them as longs; e.g.

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

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

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

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

现在我可以自由地承认,使用 int 来表示32位无符号值很棘手,并且可能容易出错。更清晰的解决方案是使用<$整个c $ c> long ,或者如果你的应用程序需要64位无符号值来使用 BigInteger

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

UPDATE - 它看起来Java 8将支持(以库方法的形式)来处理 int long 作为无符号类型 - 请参阅中的无符号整数运算API。

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

这篇关于java中的字节数组为unsigned int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆