Java基本數據類型轉換

          1 字符串轉換成數據
          字符串轉換成整數:
          String MyNumber ="1234";
          int MyInt = Integer.parseInt(MyNumber);
          字符串轉換成byte, short, int, float, double, long等數據類型,可以分別參考Byte, Short, Integer, Float, Double, Long類的parseXXX方法。

           

          2 數據轉換成字符串
          整數轉換成字符串:
          int MyInt = 1234;
          String MyString = "" + MyInt;
          其它數據類型可以利用同樣的方法轉換成字符串。

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

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

          5 整數到字節數組的轉換
          public static byte[] toByteArray(int number)
          {
          int temp = number;
          byte[] b=new byte[4];
          for (int i = b.length - 1; i > -1; i--)
          {
          b[i] = new Integer(temp & 0xff).byteValue();
          temp = temp >> 8;
          }
          return b;
          }

          6 字節數組到整數的轉換
          public static int toInteger(byte[] b)
          {
          int s = 0;
          for (int i = 0; i < 3; i++)
          {
          if (b[i] > =0)
          s = s + b[i];
          else
          s = s + 256 + b[i];
          s = s * 256;
          }
          if (b[3] > 0)
          s = s + b[3];
          else
          s = s + 256 + b[3];
          return s;
          }

          7 短整數與字節數組之間的相互轉換
          short與int之間的區別在于short是兩個字節的,而int是四個字節的。因此,只需要將5 與6 中的范例程序小做改動,即可實現短整數與字節數組之間的相互轉換。

          8 字節數組轉換成雙精度浮點數
          public double toDouble(byte[] b)
          {
          long l = 0;
          Double D = new Double(0.0);
          l = b[0];
          l |= ((long)b[1]<<8);
          l |= ((long)b[2]<<16);
          l |= ((long)b[3]<<24);
          l |= ((long)b[4]<<32);
          l |= ((long)b[5]<<40);
          l |= ((long)b[6]<<48);
          l |= ((long)b[7]<<56);
          return D.longBitsToDouble(l);
          }

          9 布爾類型轉換成字符串
          第一種方法是:

          boolean bool = true;
          String s = new Boolean(bool).toString();//將bool利用對象封裝器轉化為對象
          s.equals("true");
          /* 其中,toString方法是一個繼承方法。java中所有的類都是object的繼承,object的一個重要方法就是toString,用于將對象轉化為字符串。*/

          第二種方法是:

          boolean bool = true;
          String s = String.valueOf( bool );

          首先,從代碼長度上講第二種方法明顯要比第一種方法簡潔;其次,第一種方法在轉化過程中多引入了一個完全沒有必要的對象,因此,相對第二種方法來說這就造成了內存空間的浪費,大大減慢了運行速度。所以,推薦使用第二種方法。

          10 數字類型與數字類對象之間的轉換
          byte b = 169;
          Byte bo = new Byte( b );
          b = bo.byteValue();

          short t = 169;
          Short to = new Short( t );
          t = to.shortValue();

          int i = 169;
          Integer io = new Integer( i );
          i = io.intValue();

          long l = 169;
          Long lo = new Long( l );
          l = lo.longValue();

          float f = 169f;
          Float fo = new Float( f );
          f = fo.floatValue();

          double d = 169f;
          Double dObj = new Double( d );
          d = dObj.doubleValue();




          java二進制,字節數組,字符,十六進制,BCD編碼轉換2007-06-07 00:17/**
              * 把16進制字符串轉換成字節數組
              * 
          @param hex
              * 
          @return
              
          */

          public static byte[] hexStringToByte(String hex) {
              
          int len = (hex.length() / 2);
              
          byte[] result = new byte[len];
              
          char[] achar = hex.toCharArray();
              
          for (int i = 0; i < len; i++{
               
          int pos = i * 2;
               result[i] 
          = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
              }

              
          return result;
          }


          private static byte toByte(char c) {
              
          byte b = (byte"0123456789ABCDEF".indexOf(c);
              
          return b;
          }


          /**
              * 把字節數組轉換成16進制字符串
              * 
          @param bArray
              * 
          @return
              
          */

          public static final String bytesToHexString(byte[] bArray) {
              StringBuffer sb 
          = new StringBuffer(bArray.length);
              String sTemp;
              
          for (int i = 0; i < bArray.length; i++{
               sTemp 
          = Integer.toHexString(0xFF & bArray[i]);
               
          if (sTemp.length() < 2)
                sb.append(
          0);
               sb.append(sTemp.toUpperCase());
              }

              
          return sb.toString();
          }


          /**
              * 把字節數組轉換為對象
              * 
          @param bytes
              * 
          @return
              * 
          @throws IOException
              * 
          @throws ClassNotFoundException
              
          */

          public static final Object bytesToObject(byte[] bytes) throws IOException, ClassNotFoundException {
              ByteArrayInputStream in 
          = new ByteArrayInputStream(bytes);
              ObjectInputStream oi 
          = new ObjectInputStream(in);
              Object o 
          = oi.readObject();
              oi.close();
              
          return o;
          }


          /**
              * 把可序列化對象轉換成字節數組
              * 
          @param s
              * 
          @return
              * 
          @throws IOException
              
          */

          public static final byte[] objectToBytes(Serializable s) throws IOException {
              ByteArrayOutputStream out 
          = new ByteArrayOutputStream();
              ObjectOutputStream ot 
          = new ObjectOutputStream(out);
              ot.writeObject(s);
              ot.flush();
              ot.close();
              
          return out.toByteArray();
          }


          public static final String objectToHexString(Serializable s) throws IOException{
              
          return bytesToHexString(objectToBytes(s));
          }


          public static final Object hexStringToObject(String hex) throws IOException, ClassNotFoundException{
              
          return bytesToObject(hexStringToByte(hex));
          }


          /**
              * @函數功能: BCD碼轉為10進制串(阿拉伯數據)
              * @輸入參數: BCD碼
              * @輸出結果: 10進制串
              
          */

          public static String bcd2Str(byte[] bytes){
              StringBuffer temp
          =new StringBuffer(bytes.length*2);

              
          for(int i=0;i<bytes.length;i++){
               temp.append((
          byte)((bytes[i]& 0xf0)>>>4));
               temp.append((
          byte)(bytes[i]& 0x0f));
              }

              
          return temp.toString().substring(0,1).equalsIgnoreCase("0")?temp.toString().substring(1):temp.toString();
          }


          /**
              * @函數功能: 10進制串轉為BCD碼
              * @輸入參數: 10進制串
              * @輸出結果: BCD碼
              
          */

          public static byte[] str2Bcd(String asc) {
              
          int len = asc.length();
              
          int mod = len % 2;

              
          if (mod != 0{
               asc 
          = "0" + asc;
               len 
          = asc.length();
              }


              
          byte abt[] = new byte[len];
              
          if (len >= 2{
               len 
          = len / 2;
              }


              
          byte bbt[] = new byte[len];
              abt 
          = asc.getBytes();
              
          int j, k;

              
          for (int p = 0; p < asc.length()/2; p++{
               
          if ( (abt[2 * p] >= '0'&& (abt[2 * p] <= '9')) {
                j 
          = abt[2 * p] - '0';
               }
           else if ( (abt[2 * p] >= 'a'&& (abt[2 * p] <= 'z')) {
                j 
          = abt[2 * p] - 'a' + 0x0a;
               }
           else {
                j 
          = abt[2 * p] - 'A' + 0x0a;
               }


               
          if ( (abt[2 * p + 1>= '0'&& (abt[2 * p + 1<= '9')) {
                k 
          = abt[2 * p + 1- '0';
               }
           else if ( (abt[2 * p + 1>= 'a'&& (abt[2 * p + 1<= 'z')) {
                k 
          = abt[2 * p + 1- 'a' + 0x0a;
               }
          else {
                k 
          = abt[2 * p + 1- 'A' + 0x0a;
               }


               
          int a = (j << 4+ k;
               
          byte b = (byte) a;
               bbt[p] 
          = b;
              }

              
          return bbt;
          }

          /**
              * @函數功能: BCD碼轉ASC碼
              * @輸入參數: BCD串
              * @輸出結果: ASC碼
              
          */

          public static String BCD2ASC(byte[] bytes) {
              StringBuffer temp 
          = new StringBuffer(bytes.length * 2);

              
          for (int i = 0; i < bytes.length; i++{
               
          int h = ((bytes[i] & 0xf0>>> 4);
               
          int l = (bytes[i] & 0x0f);   
               temp.append(BToA[h]).append( BToA[l]);
              }

              
          return temp.toString() ;
          }


          /**
              * MD5加密字符串,返回加密后的16進制字符串
              * 
          @param origin
              * 
          @return
              
          */

          public static String MD5EncodeToHex(String origin) 
                 
          return bytesToHexString(MD5Encode(origin));
               }


          /**
              * MD5加密字符串,返回加密后的字節數組
              * 
          @param origin
              * 
          @return
              
          */

          public static byte[] MD5Encode(String origin){
              
          return MD5Encode(origin.getBytes());
          }


          /**
              * MD5加密字節數組,返回加密后的字節數組
              * 
          @param bytes
              * 
          @return
              
          */

          public static byte[] MD5Encode(byte[] bytes){
              MessageDigest md
          =null;
              
          try {
               md 
          = MessageDigest.getInstance("MD5");
               
          return md.digest(bytes);
              }
           catch (NoSuchAlgorithmException e) {
               e.printStackTrace();
               
          return new byte[0];
              }

            
          }

          //關于byte:    signed byte 把 0x00 ~ 0xff 映射成范圍 0~127和 -128~-1    兩段,比較簡單的辦法用 (b+256)%256的辦法令其值回到0~255,或者用&0xff并賦給一個int。參考http://www.jsfsoft.com:8080/beyond-pebble/pinxue/2006/08/23/1156309692525.html 

          posted on 2007-10-08 18:04 leoli 閱讀(1038) 評論(0)  編輯  收藏 所屬分類: java

          導航

          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          統計

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案(17)

          文章分類(86)

          收藏夾(3)

          flex blog

          good site

          java blog

          my friend

          tools

          抓蝦

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 和平区| 古交市| 洱源县| 吐鲁番市| 靖西县| 定陶县| 拉萨市| 开江县| 新和县| 汶川县| 洛南县| 犍为县| 桓台县| 海伦市| 千阳县| 成武县| 阿荣旗| 云安县| 松溪县| 五峰| 平度市| 南康市| 中西区| 龙泉市| 铜川市| 宁海县| 保靖县| 徐水县| 辽阳市| 焦作市| 大邑县| 会泽县| 福泉市| 江孜县| 石屏县| 略阳县| 凤冈县| 龙海市| 东城区| 大英县| 凌源市|