可能有人给我解释一下下面的Java code是干什么的? [英] Could someone explain to me what the following Java code is doing?
问题描述
byte s[] = getByteArray()
for(.....)
Integer.toHexString((0x000000ff & s[i]) | 0xffffff00).substring(6);
据我所知,你正试图将字节转换为十六进制字符串。我不明白的是如何做到这一点。举例来说,如果S [I]是00000001(十进制1)比你能不能解释一下:
I understand that you are trying to convert the byte into hex string. What I don't understand is how that is done. For instance if s[i] was 00000001 (decimal 1) than could you please explain:
- 为什么0x000000ff&安培; 00000001?为什么不直接使用00000001?
- 为什么从#1结果| 0xffffff00?
- 最后,为什么子(6)应用?
感谢。
推荐答案
这基本上是因为字节在Java中签署。如果你推广一个字节为int,这将签署延长,这意味着字节 0xf2
将成为 0xfffffff2
。符号扩展是加宽它时,通过复制最显著(符号)位到所有的高位比特,以保持该值相同的方法。以上两种这些值是 -14
在二进制补码。相反,如果你已经扩大 0xf2
到 0x000000f2
,这将是 242
,大概不的你想要什么。
It's basically because bytes are signed in Java. If you promote a byte to an int, it will sign extend, meaning that the byte 0xf2
will become 0xfffffff2
. Sign extension is a method to keep the value the same when widening it, by copying the most significant (sign) bit into all the higher-order bits. Both those values above are -14
in two's complement notation. If instead you had widened 0xf2
to 0x000000f2
, it would be 242
, probably not what you want.
所以&安培;
操作去掉所有的扩展位,只留下最显著8位。但是,因为你将要迫使那些位为1,下一个步骤,无论如何,这一步似乎有点浪费。
So the &
operation is to strip off any of those extended bits, leaving only the least significant 8 bits. However, since you're going to be forcing those bits to 1 in the next step anyway, this step seems a bit of a waste.
的 |以下,这将迫使所有的高位
操作为1,这样保证能让你获得 8个字符的字符串FFFFFF00
到 FFFFFFFF
包容(因为 toHexString
不给你前导零,它会翻译 7
到7
,而不是07
你想要的)。
The |
operation following that will force all those upper bits to be 1 so that you're guaranteed to get an 8-character string from ffffff00
through ffffffff
inclusive (since toHexString
doesn't give you leading zeroes, it would translate 7
into "7"
rather than the "07"
that you want).
的子(6)
,然后应用,让你只能获得最后两个的这八个十六进制数字。
The substring(6)
is then applied so that you only get the last two of those eight hex digits.
这似乎保证你一个非常令人费解的方式获得了两个字符的十六进制字符串给我时,你可以使用的String.format(%02X,S [I])
。但是,它可能是引入的String.format
时,code这个特殊的片段可能preDATE Java 5中。
It seems a very convoluted way of ensuring you get a two-character hex string to me when you can just use String.format ("%02x", s[i])
. However, it's possible that this particular snippet of code may predate Java 5 when String.format
was introduced.
如果您运行下面的程序:
If you run the following program:
public class testprog {
public static void compare (String s1, String s2) {
if (!s1.equals(s2))
System.out.println ("Different: " + s1 + " " + s2);
}
public static void main(String args[]) {
byte b = -128;
while (b < 127) {
compare (
Integer.toHexString((0x000000ff & b) | 0xffffff00).substring(6),
String.format("%02x", b, args));
b++;
}
compare (
Integer.toHexString((0x000000ff & b) | 0xffffff00).substring(6),
String.format("%02x", b, args));
System.out.println ("Done");
}
}
您将看到两位前pressions是相同的 - 它只是吐出了完成
,因为这两个前pressions产生的所有相同的结果案例。
you'll see that the two expressions are identical - it just spits out Done
since the two expressions produce the same result in all cases.
这篇关于可能有人给我解释一下下面的Java code是干什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!