public static final int parseInt(CharSequence cs, int radix, int beginIndex, int endIndex)
    {
        int size = Integer.SIZE;
        int end = endIndex;
        boolean twoComp = false;
        if (radix < 0)
        {
            twoComp = true;
            radix = -radix;
            check(cs, size, beginIndex, endIndex);
        }
        else
        {
            check(cs, radix, NumberRanges.IntRange, beginIndex, endIndex);
        }
        int result = 0;
        int sign = -1;
        int index = beginIndex;
        int cp = Character.codePointAt(cs, index);
        if (cp == '+')
        {
            if (twoComp)
            {
                throw new NumberFormatException("no signs for 2-complement "+cs.subSequence(beginIndex, endIndex));
            }
            index++;
        }
        if (cp == '-')
        {
            if (twoComp)
            {
                throw new NumberFormatException("no signs for 2-complement "+cs.subSequence(beginIndex, endIndex));
            }
            sign = 1;
            index++;
        }
        if (index >= end)
        {
            throw new NumberFormatException("unparsable number "+cs.subSequence(beginIndex, endIndex));
        }
        int count = Character.codePointCount(cs, index, end);
        if (count == size)
        {
            twoComp = false;
        }
        while (index < end)
        {
            result *= radix;
            cp = Character.codePointAt(cs, index);
            int digit = Character.digit(cp, radix);
            if (digit == -1)
            {
                throw new NumberFormatException("unparsable number "+cs.subSequence(beginIndex, endIndex));
            }
            result -= digit;
            if (Character.isBmpCodePoint(cp))
            {
                index++;
            }
            else
            {
                index += 2;
            }
        }
        if (!twoComp || -result < (1<<(count-1)))
        {
            return sign*result;
        }
        else
        {
            return -result + (-1<<count);
        }
    }
public static final int parseInt ( CharSequence cs , int radix , int beginIndex , int endIndex ) { int size = Integer . SIZE ; int end = endIndex ; boolean twoComp = false ; if ( radix < 0 ) { twoComp = true ; radix = - radix ; check ( cs , size , beginIndex , endIndex ) ; } else { check ( cs , radix , NumberRanges . IntRange , beginIndex , endIndex ) ; } int result = 0 ; int sign = - 1 ; int index = beginIndex ; int cp = Character . codePointAt ( cs , index ) ; if ( cp == '+' ) { if ( twoComp ) { throw new NumberFormatException ( "no signs for 2-complement " + cs . subSequence ( beginIndex , endIndex ) ) ; } index ++ ; } if ( cp == '-' ) { if ( twoComp ) { throw new NumberFormatException ( "no signs for 2-complement " + cs . subSequence ( beginIndex , endIndex ) ) ; } sign = 1 ; index ++ ; } if ( index >= end ) { throw new NumberFormatException ( "unparsable number " + cs . subSequence ( beginIndex , endIndex ) ) ; } int count = Character . codePointCount ( cs , index , end ) ; if ( count == size ) { twoComp = false ; } while ( index < end ) { result *= radix ; cp = Character . codePointAt ( cs , index ) ; int digit = Character . digit ( cp , radix ) ; if ( digit == - 1 ) { throw new NumberFormatException ( "unparsable number " + cs . subSequence ( beginIndex , endIndex ) ) ; } result -= digit ; if ( Character . isBmpCodePoint ( cp ) ) { index ++ ; } else { index += 2 ; } } if ( ! twoComp || - result < ( 1 << ( count - 1 ) ) ) { return sign * result ; } else { return - result + ( - 1 << count ) ; } }
Parses int from input.
<p>Input can start with '-' or '+'.
<p>Numeric value is according to radix
<p>Radix can also be -2, where input is parsed as 2-complement binary string.
Input beginning with '1' is always negative. Eg. '111' == -1, '110' == -2
@param cs
@param radix A value between Character.MIN_RADIX and Character.MAX_RADIX or -2
@param beginIndex the index to the first char of the text range.
@param endIndex the index after the last char of the text range.
@return
@throws java.lang.NumberFormatException if input cannot be parsed to proper
int.
@see java.lang.Integer#parseInt(java.lang.String, int)
@see java.lang.Character#digit(int, int)