A question in java.lang.Integer internal code

Posted by Daziplqa on Stack Overflow See other posts from Stack Overflow or by Daziplqa
Published on 2010-04-29T15:51:17Z Indexed on 2010/04/29 15:57 UTC
Read the original article Hit count: 364

Filed under:
|

Hi folks,

While looking in the code of the method:

Integer.toHexString

I found the following code :

public static String toHexString(int i) {
    return toUnsignedString(i, 4);
}

private static String toUnsignedString(int i, int shift) {
    char[] buf = new char[32];
    int charPos = 32;
    int radix = 1 << shift;
    int mask = radix - 1;
    do {
        buf[--charPos] = digits[i & mask];
        i >>>= shift;
    } while (i != 0);

    return new String(buf, charPos, (32 - charPos));
}

The question is, in toUnsignedString, why we create a char arr of 32 chars?

© Stack Overflow or respective owner

Related posts about java

Related posts about bitwise