久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

java類型之間的轉(zhuǎn)換

 李曉卡 2010-09-27
string和int之間的轉(zhuǎn)換?

字符串轉(zhuǎn)換成數(shù)據(jù)
Java代碼 復(fù)制代碼
  1. String MyNumber ="1234";    
  2. int MyInt = Integer.parseInt(MyNumber);   

字符串轉(zhuǎn)換成byte, short, int, float, double, long等數(shù)據(jù)類型,可以分別參考Byte, Short, Integer, Float, Double, Long類的parseXXX 方法,。

Java代碼 復(fù)制代碼
  1. a1=Integer.parseInt(s1);    
  2. s1=Integer.toString(a1);   


數(shù)據(jù)轉(zhuǎn)換成字符串
Java代碼 復(fù)制代碼
  1. int MyInt = 1234;    
  2. String MyString = "" + MyInt;   

其它數(shù)據(jù)類型可以利用同樣的方法轉(zhuǎn)換成字符串,。

十進(jìn)制到其他進(jìn)制的轉(zhuǎn)換
十進(jìn)制整數(shù)轉(zhuǎn)換成二進(jìn)制整數(shù),返回結(jié)果是一個(gè)字符串:
Integer.toBinaryString(int i);
Integer和Long提供了toBinaryString, toHexString和toOctalString方法,,可以方便的將數(shù)據(jù)轉(zhuǎn)換成二進(jìn)制,、十六進(jìn)制和八進(jìn)制字符串。功能更加強(qiáng)大的是其toString(int/long i, int radix)方法,,可以將一個(gè)十進(jìn)制數(shù)轉(zhuǎn)換成任意進(jìn)制的字符串形式,。
byte, short, float和double等數(shù)據(jù)類型,可以利用Integer或者是Long的toBinaryString, toHexString, to OctalString和toString方法轉(zhuǎn)換成其他進(jìn)制的字符串形式,。

其它進(jìn)制到十進(jìn)制的轉(zhuǎn)換
五進(jìn)制字符串14414轉(zhuǎn)換成十進(jìn)制整數(shù),,結(jié)果是1234:
System.out.println(Integer.valueOf("14414", 5);
Integer和Long提供的valueOf(String source, int radix)方法,可以
將任意進(jìn)制的字符串轉(zhuǎn)換成十進(jìn)制數(shù)據(jù),。

把String類型轉(zhuǎn)換成16進(jìn)制的整數(shù)
Java代碼 復(fù)制代碼
  1. public static void main(String args[]){   
  2. String x = "0x300C8";   
  3. int y = Integer.decode(x).intvalue();   
  4. System.out.println(y);   
  5. }  


int,、char、double與byte相互轉(zhuǎn)換的程序
整數(shù)到字節(jié)數(shù)組的轉(zhuǎn)換
Java代碼 復(fù)制代碼
  1. public static byte[] intToByte(int number) {   
  2.   int temp = number;   
  3.   byte[] b=new byte[4];   
  4.   for (int i=b.length-1;i>-1;i--){   
  5.     b[i] = new Integer(temp&0xff).byteValue();      //將最高位保存在最低位   
  6.     temp = temp >> 8;       //向右移8位   
  7.   }   
  8.   return b;   
  9. }  


  字節(jié)數(shù)組到整數(shù)的轉(zhuǎn)換
 
Java代碼 復(fù)制代碼
  1. public static int byteToInt(byte[] b) {   
  2.     int s = 0;   
  3.     for (int i = 0; i < 3; i++) {   
  4.       if (b[i] >= 0)   
  5.         s = s + b[i];   
  6.       else  
  7.         s = s + 256 + b[i];   
  8.       s = s * 256;   
  9.     }   
  10.     if (b[3] >= 0)       //最后一個(gè)之所以不乘,,是因?yàn)榭赡軙?huì)溢出   
  11.       s = s + b[3];   
  12.     else  
  13.       s = s + 256 + b[3];   
  14.     return s;   
  15.   }  


短整數(shù)與字節(jié)數(shù)組之間的相互轉(zhuǎn)換
short與int之間的區(qū)別在于short是兩個(gè)字節(jié)的,而int是四個(gè)字節(jié)的,。因此,,只需要將5 與6 中的范例程序小做改動(dòng),即可實(shí)現(xiàn)短整數(shù)與字節(jié)數(shù)組之間的相互轉(zhuǎn)換。


  字符到字節(jié)轉(zhuǎn)換
 
Java代碼 復(fù)制代碼
  1. public static byte[] charToByte(char ch){   
  2.     int temp=(int)ch;   
  3.     byte[] b=new byte[2];   
  4.     for (int i=b.length-1;i>-1;i--){   
  5.       b[i] = new Integer(temp&0xff).bytevalue();      //將最高位保存在最低位   
  6.       temp = temp >> 8;       //向右移8位   
  7.     }   
  8.     return b;   
  9.   }  


  //字節(jié)到字符轉(zhuǎn)換
 
Java代碼 復(fù)制代碼
  1. public static char byteToChar(byte[] b){   
  2.     int s=0;   
  3.     if(b[0]>0)   
  4.       s+=b[0];   
  5.     else  
  6.       s+=256+b[0];   
  7.     s*=256;   
  8.     if(b[1]>0)   
  9.       s+=b[1];   
  10.     else  
  11.       s+=256+b[1];   
  12.     char ch=(char)s;   
  13.     return ch;   
  14.   }  


  浮點(diǎn)到字節(jié)轉(zhuǎn)換
 
Java代碼 復(fù)制代碼
  1. public static byte[] doubleToByte(double d){   
  2.     byte[] b=new byte[8];   
  3.     long l=Double.doubleToLongBits(d);   
  4.     for(int i=0;i<b.length;i++){   
  5.       b[i]=new Long(l).bytevalue();   
  6.       l=l>>8;   
  7.     }   
  8.     return b;   
  9.   }  


  字節(jié)到浮點(diǎn)轉(zhuǎn)換
 
Java代碼 復(fù)制代碼
  1. public static double byteToDouble(byte[] b){   
  2.     long l;   
  3.     l=b[0];   
  4.     l&=0xff;   
  5.     l|=((long)b[1]<<8);   
  6.     l&=0xffff;   
  7.     l|=((long)b[2]<<16);   
  8.     l&=0xffffff;   
  9.     l|=((long)b[3]<<24);   
  10.     l&=0xffffffffl;   
  11.     l|=((long)b[4]<<32);   
  12.     l&=0xffffffffffl;   
  13.     
  14.     l|=((long)b[5]<<40);   
  15.     l&=0xffffffffffffl;   
  16.     l|=((long)b[6]<<48);   
  17.   
  18.   
  19.     l|=((long)b[7]<<56);   
  20.     return Double.longBitsToDouble(l);   
  21.   }  


int與byte array之間的轉(zhuǎn)換程序
在通訊中經(jīng)常需要將數(shù)值轉(zhuǎn)換成字節(jié)流,,或者是將字節(jié)流轉(zhuǎn)換成數(shù)值,。下面
提供的程序可以進(jìn)行int和byte array之間的轉(zhuǎn)換。

Java代碼 復(fù)制代碼
  1. /**  
  2.  *  
  3.  * IntConverter  
  4.  *  
  5.  * This class provides methods to convert int into byte array and  
  6.  * byte array back into int.  
  7.  *  
  8.  */  
  9. public class IntConverter   
  10. {   
  11.         /**  
  12.          *  
  13.          * Method converting int into byte array.  
  14.          *  
  15.          * @param number The int value to be converted.  
  16.          *  
  17.          */  
  18.         public static byte[] toByteArray(int number)   
  19.         {   
  20.             int temp = number;   
  21.             byte[] b=new byte[4];   
  22.             for (int i = b.length - 1; i > -1; i--)   
  23.             {   
  24.                 b[i] = new Integer(temp & 0xff).bytevalue();   
  25.                 temp = temp >> 8;   
  26.             }   
  27.             return b;   
  28.         }   
  29.     
  30.         /**  
  31.          *  
  32.          * Method converting byte array into int.  
  33.          *  
  34.          * @param The byte array to be converted.  
  35.          *  
  36.          */  
  37.         public static int toInteger(byte[] b)   
  38.         {   
  39.             int s = 0;   
  40.             for (int i = 0; i < 3; i++)   
  41.             {   
  42.                 if (b[i] > 0)   
  43.                     s = s + b[i];   
  44.                 else  
  45.                     s = s + 256 + b[i];   
  46.                 s = s * 256;   
  47.             }   
  48.             if (b[3] > 0)   
  49.                 s = s + b[3];   
  50.             else  
  51.                 s = s + 256 + b[3];   
  52.             return s;   
  53.         }   
  54.     
  55.     // Testing program.   
  56.     public static void main(String[] args)   
  57.     {   
  58.         IntConverter abc = new IntConverter();   
  59.         int s = -1121115678;   
  60.         byte[] b = abc.toByteArray(s);   
  61.         for (int i = 0; i <= 3; i++)   
  62.                 System.out.println(b[i]);   
  63.         s = abc.toInteger(b);   
  64.         System.out.println(s);   
  65.     }   
  66. }  


字節(jié)數(shù)組到整數(shù)的轉(zhuǎn)換

Java代碼 復(fù)制代碼
  1. public static int byteToint(byte[] convertByteValue)   
  2.      { byte[] YY=new byte[4];   
  3.       YY=convertByteValue;   
  4.       int ee, ff, gg, hh;   
  5.       ee = YY[3] & 0x000000ff;   
  6.       //System.out.println("ee: " +ee);   
  7.       ff = (YY[2]<<8) & 0x0000ff00;   
  8.       //System.out.println("ff: " +ff);   
  9.       gg = (YY[1]<<16) & 0x00ff0000;   
  10.       // System.out.println("gg: " +gg);   
  11.       hh = YY[0]<<24;   
  12.       // System.out.println("hh: "+hh);   
  13.       int jj = ee + ff + gg + hh;   
  14.       // System.out.println("jj: "+jj);   
  15.       return jj;   
  16.       }  


    整數(shù)到字節(jié)數(shù)組的轉(zhuǎn)換

  
Java代碼 復(fù)制代碼
  1. public byte[] intTobyte(int convertIntValue)   
  2.     { int Y;   
  3.       Y = 1321432453;   
  4.       byte YY[] = new byte[4];   
  5.       Integer aa = new Integer(Y);   
  6.        YY[3] = aa.byteValue();   
  7.       Integer bb = new Integer(Y>>>8);   
  8.        YY[2] = bb.byteValue();   
  9.       Integer cc = new Integer(Y>>>16);   
  10.        YY[1] = cc.byteValue();   
  11.       Integer dd = new Integer(Y>>>24);   
  12.        YY[0] = dd.byteValue();   
  13.        return YY;   
  14.        }  

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購(gòu)買等信息,,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請(qǐng)點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多