Как Integer.parseInt (представляет в виде строки) на самом деле работу?

Ответы, которые дают люди, являются большими. Большая часть этого получена в итоге в svn пользовательском документе для лучшие практики для SVN.
Для повторения:

  1. Настроенный Ваша структура репозитория (у Вас должен быть корень проекта с соединительной линией, ответвлениями и тегами внизу)
  2. Выбирают Ваше ветвление ре политики (частные ответвления, ответвления в этап/выпуск/ошибку, и т.д.) и придерживаются его - я рекомендовал бы больше ветвления, а не меньше, но никакая потребность в частных ответвлениях
  3. Не Выбирает Ваши метки ре политики - больше тегов лучше, но самое главное выбирает Ваши соглашения о присвоении имен тега
  4. , Выбирают, Ваша передача ре политики соединительной линии - сохраняют соединительную линию максимально "чистой", это должно быть способно выпуском в любое время

28
задан Karl 11 September 2009 в 12:24
поделиться

4 ответа

Usually this is done like this:

  • init result with 0
  • for each character in string do this
    • result = result * 10
    • get the digit from the character ('0' is 48 ASCII (or 0x30), so just subtract that from the character ASCII code to get the digit)
    • add the digit to the result
  • return result

Edit: This works for any base if you replace 10 with the correct base and adjust the obtaining of the digit from the corresponding character (should work as is for bases lower than 10, but would need a little adjusting for higher bases - like hexadecimal - since letters are separated from numbers by 7 characters).

Edit 2: Char to digit value conversion: characters '0' to '9' have ASCII values 48 to 57 (0x30 to 0x39 in hexa), so in order to convert a character to its digit value a simple subtraction is needed. Usually it's done like this (where ord is the function that gives the ASCII code of the character):

digit = ord(char) - ord('0')

For higher number bases the letters are used as 'digits' (A-F in hexa), but letters start from 65 (0x41 hexa) which means there's a gap that we have to account for:

digit = ord(char) - ord('0')
if digit > 9 then digit -= 7

Example: 'B' is 66, so ord('B') - ord('0') = 18. Since 18 is larger than 9 we subtract 7 and the end result will be 11 - the value of the 'digit' B.

One more thing to note here - this works only for uppercase letters, so the number must be first converted to uppercase.

42
ответ дан 28 November 2019 в 02:30
поделиться

I'm not sure what you're looking for, as "high level". I'll give it a try:

  • take the String, parse all characters one by one
  • start with a total of 0
  • if it is between 0 and 9, total = (total x 10) + current
  • when done, the total is the result
8
ответ дан 28 November 2019 в 02:30
поделиться
  • Найдите длину строки (строк) (скажем, maxSize)
  • Инициализировать результат = 0
  • цикл начала (int j = maxSize, i = 0 ; j> 0; j--, i ++)
  • int digit = Character.digit (s.charAt (i))
  • result = result + digit * (10 степень j-1)
  • end loop
  • вернуть результат
3
ответ дан 28 November 2019 в 02:30
поделиться

The source code of the Java API is freely available. Here's the parseInt() method. It's rather long because it has to handle a lot of exceptional and corner cases.

public static int parseInt(String s, int radix)
    throws NumberFormatException
{
    if (s == null) {
        throw new NumberFormatException("null");
    }

if (radix < Character.MIN_RADIX) {
    throw new NumberFormatException("radix " + radix +
                    " less than Character.MIN_RADIX");
}

if (radix > Character.MAX_RADIX) {
    throw new NumberFormatException("radix " + radix +
                    " greater than Character.MAX_RADIX");
}

int result = 0;
boolean negative = false;
int i = 0, max = s.length();
int limit;
int multmin;
int digit;

if (max > 0) {
    if (s.charAt(0) == '-') {
    negative = true;
    limit = Integer.MIN_VALUE;
    i++;
    } else {
    limit = -Integer.MAX_VALUE;
    }
    multmin = limit / radix;
    if (i < max) {
    digit = Character.digit(s.charAt(i++),radix);
    if (digit < 0) {
        throw NumberFormatException.forInputString(s);
    } else {
        result = -digit;
    }
    }
    while (i < max) {
    // Accumulating negatively avoids surprises near MAX_VALUE
    digit = Character.digit(s.charAt(i++),radix);
    if (digit < 0) {
        throw NumberFormatException.forInputString(s);
    }
    if (result < multmin) {
        throw NumberFormatException.forInputString(s);
    }
    result *= radix;
    if (result < limit + digit) {
        throw NumberFormatException.forInputString(s);
    }
    result -= digit;
    }
} else {
    throw NumberFormatException.forInputString(s);
}
if (negative) {
    if (i > 1) {
    return result;
    } else {    /* Only got "-" */
    throw NumberFormatException.forInputString(s);
    }
} else {
    return -result;
}
}
25
ответ дан 28 November 2019 в 02:30
поделиться
Другие вопросы по тегам:

Похожие вопросы: