Java数据类型转换

 
1       字符串to整型
String num = "111";
int Integer.parseInt(num);       // 确保num 只有数字字符
1.1    byte and string
public class TestMain {
 
    public static String byte2string( byte [] input){
       String returnString = new String(input);
      
       return returnString;
    }
   
    public static byte [] string2byte(String input){
       byte [] output = input.getBytes();
      
       return output;
    }
   
    /**
      * byte2string , 如果 byte 定义过大 , 则会有 '/0' 字符显示问题 ,'/0' 显示是一个 " 口口口口口口 "
      * @param args
      */
    public static void process0 的问题 (){
       String source = "AABB 中国 CC" ;
       String dest = null ;
       byte [] bt1 = source.getBytes();
       byte [] bt2 = new byte [32];
      
       // 显示是一个 " 口口口口口口 "
       for ( int i = 0; i < bt1. length ; i ++){
           bt2[i] = bt1[i];
       }
       dest = new String(bt2);
       System. out .println( "bt2 = " + dest);
      
       // 显示没有 " 口口口口口口 "
       byte [] bt3 = new byte [bt1. length ];
       for ( int i = 0; i < bt1. length ; i ++){
           bt3[i] = bt1[i];
       }
       dest = new String(bt3);
       System. out .println( "bt3 = " + dest);
    }
    public static void 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 的问题 ();
      
    }
}
 
 

你可能感兴趣的:(Java数据类型转换)