String和数字之间的转化

主要是JDK的代码,还是比较的经典,值得一看,例如:

package alg;

/**
 * @author zha 字符串之间的转化
 */
public class Alg3StringToint {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String intv = "1232192373290";
//        int value = Integer.parseInt(intv);
//        System.out.println(value);
    }

    // JDK的源码还是比较好的研究对象
    public static int parseInt(String s, int radix)
            throws NumberFormatException {
        /*
         * WARNING: This method may be invoked early during VM initialization
         * before IntegerCache is initialized. Care must be taken to not use the
         * valueOf method.
         */

        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, len = s.length();
        int limit = -Integer.MAX_VALUE;
        int multmin;
        int digit;

        if (len > 0) {
            char firstChar = s.charAt(0);
            if (firstChar < ‘0‘) { // Possible leading "+" or "-"
                if (firstChar == ‘-‘) {
                    negative = true;
                    limit = Integer.MIN_VALUE;
                } else if (firstChar != ‘+‘)
                    throw new NumberFormatException(s);

                if (len == 1) // Cannot have lone "+" or "-"
                    throw new NumberFormatException(s);
                i++;
            }
            multmin = limit / radix;
            while (i < len) {
                // Accumulating negatively avoids surprises near MAX_VALUE
                digit = Character.digit(s.charAt(i++), radix);
                if (digit < 0) {
                    throw new NumberFormatException(s);
                }
                if (result < multmin) {
                    throw new NumberFormatException(s);
                }
                result *= radix;
                if (result < limit + digit) {
                    throw new NumberFormatException(s);
                }
                result -= digit;
            }
        } else {
            throw new NumberFormatException(s);
        }
        return negative ? result : -result;
    }
}
时间: 2024-08-27 05:57:01

String和数字之间的转化的相关文章

string,char*,int 之间的转化

c++中经常遇到string,char*,int之间的相互转化,今天就来整理一下. 以下是转载并修改的内容: 以下是常用的几种类型互相之间的转换 string 转 int先转换为char*,再使用atoi()函数,具体如下 .............................. char* 转 int #include <stdlib.h> int atoi(const char *nptr); long atol(const char *nptr); long long atoll(c

C++ 11特性中string与数字之间的互相转换

在写C++时经常会遇到字符串string与int等数字之间的相互转化,在java这种语言特性特别丰富的语言中能够通过Integer和String包装类中的函数parseInt和valueOf等函数轻松转换,而在C++中一直没有特别好的处理办法. 在之前的经历中,我都是通过C++中的字符串流stringstream来进行转换的,即通过如下函数: 1 int str2int(string input){ 2 stringstream ss; 3 ss<<input; 4 int res; 5 ss

字符串和数字之间的转化

1. 由 基本数据型态转换成 StringString 类别中已经提供了将基本数据型态转换成 String 的 static 方法也就是 String.valueOf() 这个参数多载的方法有下列几种String.valueOf(boolean b) : 将 boolean 变量 b 转换成字符串String.valueOf(char c) : 将 char 变量 c 转换成字符串String.valueOf(char[] data) : 将 char 数组 data 转换成字符串String.

string,CString,char*之间的转化

int 转 CString:CString.Format("%d",int);...............................string 转 CString CString.format("%s", string.c_str()); 用c_str()确实比data()要好. .......................................char* 转 CString CString.format("%s", cha

c++数字和字符之间的转化

关于C++中数与字符之间的转化 在c++中我们经常遇到需要把一个数变成字符,或者把字符变为一个数,c++中没有直接的转化函数,故我们需要自己去写函数去转化,这里我将介绍两种比较简单的方法: 法一: sprintf()函数把一个数字如何变成一个字符串: float a=23.309774; char ss[8]; sprintf(ss,"%lf",a); 使用这种方法直接将数字a变成了字符串ss,值得注意的是,这种是把数字转化为数组,可以通过ss[i]这种方式提取任何元素,ss数组的大小

js数字、字符串、数组之间的转化

1.数组转字符串 var a, b; a = new Array(0,1,2,3,4); b = a.join("-"); 2.字符串转数组 var s = "abc,abcd,aaa"; ss = s.split(","); 3.数字转字符串toString var i = 10; var s = i.toString(); 4.字符串转数字 parseInt(字符串) parseInt("ssss999"); //错误 输

Java中InputStream和String之间的转化

https://blog.csdn.net/lmy86263/article/details/60479350 在Java中InputStream和String之间的转化十分普遍,本文主要是总结一下转换的各种方法,包括JDK原生提供的,还有一些外部依赖提供的.1.InputStream转化为String1.1 JDK原生提供 方法一:byte[] bytes = new byte[0];bytes = new byte[inputStream.available()];inputStream.r

C# char[]与string之间的转换 byte[]与string之间的转化

1.char[]与string之间的转换 //string 转换成 Char[] string str="hello"; char[] arr=str.ToCharArray(); //Char[] 转换成 string string str1 = new string(arr); 2.byte[]与string之间的转化 string str = "你好,hello"; byte[] bytes; //byte[] 转换成 string bytes = Encod

java,数字,字符,字符串之间的转化

首先,先看一道编程题目: A除以B (20) 时间限制 1000 ms 内存限制 32768 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数.你需要输出商数Q和余数R,使得A = B * Q + R成立. 输入描述: 输入在1行中依次给出A和B,中间以1空格分隔. 输出描述: 在1行中依次输出Q和R,中间以1空格分隔. 输入例子: 123456789050987654321 7 输出例子