Here you can find the source of atoi(String s, int pos, int base)
Parameter | Description |
---|---|
s | the string with a number in it. |
pos | the starting position in the string to find the number. |
base | the forced base of the number (0 to determine it automatically). |
public static int atoi(String s, int pos, int base)
//package com.java2s; /* -*- tab-width: 4 -*-/*from www . java 2 s .c om*/ * * Electric(tm) VLSI Design System * * File: TextUtils.java * * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved. * * Electric(tm) is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * Electric(tm) is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Electric(tm); see the file COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, Mass 02111-1307, USA. */ public class Main { /** * Method to parse the number in a string. * <P> * There are many reasons to use this method instead of Integer.parseInt... * <UL> * <LI>This method can handle any radix. * If the number begins with "0", presume base 8. * If the number begins with "0b", presume base 2. * If the number begins with "0x", presume base 16. * Otherwise presume base 10. * <LI>This method can handle numbers that affect the sign bit. * If you give 0xFFFFFFFF to Integer.parseInt, you get a numberFormatPostFix exception. * This method properly returns -1. * <LI>This method does not require that the entire string be part of the number. * If there is extra text after the end, Integer.parseInt fails (for example "123xx"). * <LI>This method does not throw an exception if the number is invalid (or blank). * </UL> * @param s the string with a number in it. * @return the numeric value. */ public static int atoi(String s) { return atoi(s, 0, 0); } /** * Method to parse the number in a string. * See the comments for "atoi(String s)" for reasons why this method exists. * @param s the string with a number in it. * @param pos the starting position in the string to find the number. * @return the numeric value. */ public static int atoi(String s, int pos) { return atoi(s, pos, 0); } /** * Method to parse the number in a string. * See the comments for "atoi(String s)" for reasons why this method exists. * @param s the string with a number in it. * @param pos the starting position in the string to find the number. * @param base the forced base of the number (0 to determine it automatically). * @return the numeric value. */ public static int atoi(String s, int pos, int base) { int num = 0; int sign = 1; int len = s.length(); if (pos < len && s.charAt(pos) == '-') { pos++; sign = -1; } if (base == 0) { base = 10; if (pos < len && s.charAt(pos) == '0') { pos++; base = 8; if (pos < len && (s.charAt(pos) == 'x' || s.charAt(pos) == 'X')) { pos++; base = 16; } else if (pos < len && (s.charAt(pos) == 'b' || s.charAt(pos) == 'B')) { pos++; base = 2; } } } for (; pos < len; pos++) { char cat = s.charAt(pos); int digit = Character.digit(cat, base); if (digit < 0) { break; } num = num * base + digit; // if ((cat >= 'a' && cat <= 'f') || (cat >= 'A' && cat <= 'F')) // { // if (base != 16) break; // num = num * 16; // if (cat >= 'a' && cat <= 'f') num += cat - 'a' + 10; else // num += cat - 'A' + 10; // continue; // } // if (!TextUtils.isDigit(cat)) break; // if (cat >= '8' && base == 8) break; // num = num * base + cat - '0'; } return (num * sign); } private static int digit(char ch) { if (ch < '\u0080') { return ch >= '0' && ch <= '9' ? ch - '0' : -1; } return Character.digit((int) ch, 10); } }