閭d箞鎴戜滑鐪嬬湅jdk緇欏嚭鐨勫疄鐜幫細
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;
}
}
榪囩▼灝辨槸鎸夌収鎬濊礬鏉ョ殑錛屼絾鏄洿鍏ㄩ潰涓浜涳紝棣栧厛鍋氫竴浜涘弬鏁版鏌ワ紝鐒跺悗瀹氫箟浜嗗眬閮ㄥ彉閲忕敤浜庤綆楋細result鏄搴旂殑int緇撴灉錛宯egative瀵瑰簲鏄惁鏄礋鏁扮殑鍒ゆ柇錛宨鏄亶鍘嗙敤鐨勭儲寮曟寚閽堬紝max浠h〃瀛楃涓茬殑闀垮害錛宭imit鏄悎娉曟暟瀛楃殑涓婇檺錛堜笅闄愶級錛宒igit鏄綋鍓嶆壂鎻忓埌鐨勫瓧絎﹀搴旂殑鏁板瓧錛宮ultmin鏄湪鍋氫箻娉曡綆楁椂鑳借蛋鍒扮殑鍚堟硶涓嬮檺銆?/p>
涓ヨ皚鏄繖孌電▼搴忔渶澶х殑鐗圭偣錛屽洜涓烘湁絎﹀彿int鐨勪笂涓嬮檺鏄?2147483648~2147483647錛屽彲瑙佽礋鏁拌〃杈劇殑鑼冨洿姣旀鏁板涓涓紝榪欐牱灝卞ソ鐞嗚В涓轟粈涔堝湪寮澶磋鎶妉imit鍏ㄩ儴琛ㄨ揪涓鴻礋鏁幫紙涓嬮檺錛夛紝榪欐牱鐨勬搷浣滃噺灝戜簡鍚庣畫鐨勫垽鏂紝鍙互涓姝ュ埌浣嶏紝鐩稿綋浜庝簩鑰呴夋嫨鍙栧叾澶т竴鏍鳳紝澶х殑鍖呭惈浜嗗皬鐨勩傚悓鐞嗭紝閭d箞multmin涔熷氨鏄礋鏁頒簡錛岃屼笖鍙互璁や負鏄彧鍜岃繘鍒跺弬鏁皉adix鏈夊叧緋匯傛帴鐫姣忎釜char鐨勬壂鎻忚綆梔igit鍒╃敤鍒頒簡Character.digit(char,int) 鏂規硶錛岃繖涓柟娉曞氨鏄湪璋冪敤CharacterDataLatin1.digit(codePoint, radix) 鏂規硶錛岃岃繖涓柊鐨勬柟娉曞叾瀹炲彧鏄幓闈欐佹暟緇勪腑鍙栦釜鏄犲皠鑰屽凡銆傛渶鍚庡綋欏哄埄鐨勬墽琛屽畬while寰幆鍚庯紝result緇撴灉涔熷氨璁$畻濂戒簡銆?/p>
浣滀負紼嬪簭璁捐浜哄憳錛屾垜鏈鍒濇帴瑙︾殑璇█鏄疌++錛屽綋鍒濈敤鍒扮殑搴撳嚱鏁版槸atoi錛岄偅涔堟垜浠湅鐪媋toi鐨勫簱鏍囧噯瀹炵幇錛?/p>
int atoi(str) const char *str; { _DIAGASSERT(str != NULL); return((int)strtol(str, (char **)NULL, 10)); }
鍏朵腑璋冪敤浜唖trtol鏂規硶錛屽弬鏁頒紶閫掔殑radix鏄?0錛屼篃灝辨槸璇存垜浠父鐢ㄧ殑atoi鏄粯璁よ漿鍖栧瓧絎︿覆鍒?0榪涘埗鐨勩傚叾涓紑濮嬫椂榪樿繘琛屼簡涓涓猼rim鐨勬搷浣滐紝鑰屼笖鏀寔16榪涘埗鐨?x寮澶達紝鍙皳瀹屽叏鐨勫敖鍠勫敖緹庡晩銆?/font>
strtol鏂規硶錛?/font>
#define _FUNCNAME strtol #define __INT long #define __INT_MIN LONG_MIN #define __INT_MAX LONG_MAX
__INT _FUNCNAME(const char *nptr, char **endptr, int base) { const char *s; __INT acc, cutoff; char c; int i, neg, any, cutlim; _DIAGASSERT(nptr != NULL); /* endptr may be NULL */ /* check base value */ if (base && (base < 2 || base > 36)) { errno = EINVAL; return(0); } /* * Skip white space and pick up leading +/- sign if any. * If base is 0, allow 0x for hex and 0 for octal, else * assume decimal; if base is already 16, allow 0x. */ s = nptr; do { c = *s++; } while (isspace(c)); if (c == '-') { neg = 1; c = *s++; } else { neg = 0; if (c == '+') c = *s++; } if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) { c = s[1]; s += 2; base = 16; } if (base == 0) base = c == '0' ? 8 : 10; /* * Compute the cutoff value between legal numbers and illegal * numbers. That is the largest legal value, divided by the * base. An input number that is greater than this value, if * followed by a legal input character, is too big. One that * is equal to this value may be valid or not; the limit * between valid and invalid numbers is then based on the last * digit. For instance, if the range for longs is * [-2147483648..2147483647] and the input base is 10, * cutoff will be set to 214748364 and cutlim to either * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated * a value > 214748364, or equal but the next digit is > 7 (or 8), * the number is too big, and we will return a range error. * * Set any if any `digits' consumed; make it negative to indicate * overflow. */ cutoff = neg ? __INT_MIN : __INT_MAX; cutlim = (int)(cutoff % base); cutoff /= base; if (neg) { if (cutlim > 0) { cutlim -= base; cutoff += 1; } cutlim = -cutlim; } for (acc = 0, any = 0;; c = *s++) { if (!isascii(c)) break; if (isdigit(c)) i = c - '0'; else if (isalpha(c)) i = c - (isupper(c) ? 'A' - 10 : 'a' - 10); else break; if (i >= base) break; if (any < 0) continue; if (neg) { if (acc < cutoff || (acc == cutoff && i > cutlim)) { any = -1; acc = __INT_MIN; errno = ERANGE; } else { any = 1; acc *= base; acc -= i; } } else { if (acc > cutoff || (acc == cutoff && i > cutlim)) { any = -1; acc = __INT_MAX; errno = ERANGE; } else { any = 1; acc *= base; acc += i; } } } if (endptr != 0) /* LINTED interface specification */ *endptr = __DECONST(char *, (any ? s - 1 : nptr)); return(acc); }
褰撶劧錛岀被浼肩殑浠g爜榪樻湁寰堝錛岃繖閲屽彧鍒楀嚭浜嗕袱澶ц璦鐨勫簱瀹炵幇錛屾諱綋鎬濊礬鏄竴鑷寸殑錛屽綋鎴戜滑璁捐api鏃訛紝榪欑緙栫▼鎬濊礬鍜岄鏍間互鍙婂姛鑳界殑鑰冭檻鏄垜浠渶瑕佸涔犵殑銆?/font>
涓嬮潰榪欎袱綃噑tackoverflow鐨勯棶絳旂粰鍑轟簡涓浜涙瘮杈冨叏闈㈢殑c椋庢牸浠g爜錛屽彲浠ュ弬鑰冿紝榪欓噷涓嶈創鍏ㄦ枃鍙粰link錛?/font>
http://stackoverflow.com/questions/194465/how-to-parse-a-string-to-an-int-in-c
http://stackoverflow.com/questions/4442658/c-parse-int-from-string
鍙傝冩枃鐚細
jdk鏂囨。鍙婃簮鐮?/font>
c搴撳嚱鏁版簮鐮佸強鏂囨。