现在的位置: 首页 > 综合 > 正文

Java数据类型转换

2014年02月15日 ⁄ 综合 ⁄ 共 1403字 ⁄ 字号 评论关闭
 

1       字符串to整型
String num = "111";
int Integer.parseInt(num);      //确保num只有数字字符
1.1    byte and string
publicclass TestMain {
 
    publicstatic String byte2string(byte[] input){
       String returnString = new String(input);
      
       return returnString;
    }
   
    publicstaticbyte[] string2byte(String input){
       byte [] output = input.getBytes();
      
       return output;
    }
   
    /**
     *byte2string,如果byte定义过大,则会有'/0'字符显示问题,'/0'显示是一个"口口口口口口"
     *@paramargs
     */
    publicstaticvoid process0的问题(){
       String source = "AABB中国CC";
       String dest = null;
       byte[] bt1 = source.getBytes();
       byte[] bt2 = newbyte[32];
      
       //显示是一个"口口口口口口"
       for(int i = 0; i < bt1.length; i ++){
           bt2[i] = bt1[i];
       }
       dest = new String(bt2);
       System.out.println("bt2 = " + dest);
      
       //显示没有"口口口口口口"
       byte[] bt3 = newbyte[bt1.length];
       for(int i = 0; i < bt1.length; i ++){
           bt3[i] = bt1[i];
       }
       dest = new String(bt3);
       System.out.println("bt3 = " + dest);
    }
    publicstaticvoid main(String args[]){
       String test = "AABB中国CC";
      
       //byte反映了字符真实的长度,byte[].length=10
       byte [] ret = TestMain.string2byte(test);
       System.out.println("byte = " + ret + ",/tbyte[].length = " + ret.length);
       //string反映了字符的个数,一个汉字算一个字符,string.length()=8
       String str = TestMain.byte2string(ret);
       System.out.println("string = " + str + ",/tstring.length() = " + str.length());
      
       //byte2string的显示问题
       TestMain.process0的问题();
      
    }
}
 

 

抱歉!评论已关闭.