posts - 40, comments - 58, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          byte[] to int

          Posted on 2010-11-12 12:09 Astro.Qi 閱讀(1715) 評論(0)  編輯  收藏 所屬分類: Java
          public class NumberBytesUtils {

          public static void main(String[] args) {
          byte[] bytes = new byte[4];
          bytes[0] = (byte) 65;
          bytes[1] = (byte) 0;
          bytes[2] = (byte) 97;
          bytes[3] = (byte) 0;
          System.out.println("String: " + new String(bytes));
          int iv = bytesToInt(bytes);
          float fv = bytesToFloat(bytes);
          byte[] bs = intToBytes(iv);
          byte[] fs = floatToBytes(fv);
          System.out.println("Int: " + iv);
          System.out.println("Float: " + fv);
          System.out.println("------------");
          for (int i = 0; i < bs.length; i++) {
          System.out.println(bs[i]);
          }
          System.out.println("============");
          for (int i = 0; i < fs.length; i++) {
          System.out.println(fs[i]);
          }
          System.out.println("============");
          System.out.println(bytesToFloat(floatToBytes(-0.45367f)));
          System.out.println("************");
          // System.out.println(0xff);
          // System.out.println(0xff00);
          // System.out.println(0xff0000);
          // System.out.println(0xff000000);
          byte[] bytesL = new byte[8];
          System.arraycopy(bytes, 0, bytesL, 0, bytes.length);
          bytesL[4] = (byte) 0;
          bytesL[5] = (byte) 0;
          bytesL[6] = (byte) 0;
          bytesL[7] = (byte) 0;
          long lv = bytesToLong(bytesL);
          double dv = bytesToDouble(bytesL);
          byte[] bls = longToBytes(lv);
          byte[] dls = doubleToBytes(dv);
          System.out.println("Long: " + lv);
          System.out.println("Double: " + dv);
          System.out.println("----");
          for (int i = 0; i < bls.length; i++) {
          System.out.println(bls[i]);
          }
          System.out.println("----");
          for (int i = 0; i < dls.length; i++) {
          System.out.println(dls[i]);
          }
          System.out.println("****");
          System.out.println(bytesToDouble(doubleToBytes(2.345)));
          }
          /**
          * bytes[3] = value >> 24
          * bytes[2] = value >> 16
          * bytes[1] = value >>  8
          * bytes[0] = value >>  0
          * @param value
          * @return
          */
          public static byte[] intToBytes(int value){
          int length = 4;
          byte[] bytes = new byte[length];
          for (int i = length - 1; i >= 0; i--) {
          int offset = i * 8; // 24, 16, 8
          bytes[i] = (byte) (value >> offset);
          }
          return bytes;
          }
          /**
          * bytes[7] = value >> 56
          * bytes[6] = value >> 48
          * bytes[5] = value >> 40
          * bytes[4] = value >> 32
          * bytes[3] = value >> 24
          * bytes[2] = value >> 16
          * bytes[1] = value >>  8
          * bytes[0] = value >>  0
          * @param value
          * @return
          */
          public static byte[] longToBytes(long value){
          int length = 8;
          byte[] bytes = new byte[length];
          for (int i = length - 1; i >= 0; i--) {
          int offset = i * 8; //56, 48, 40, 32, 24, 16, 8
          bytes[i] = (byte) (value >> offset);
          }
          return bytes;
          }

          /**
          * 操作符 << 的優先級比 & 高
          * intValue = (bytes[3] & 0xFF) << 24
                 | (bytes[2] & 0xFF) << 16
                 | (bytes[1] & 0xFF) <<  8
                 | (bytes[0] & 0xFF) <<  0
          * @param bytes
          * @return
          */
          public static int bytesToInt (byte[] bytes){
          int length = 4;
          int intValue = 0;
                  for (int i = length - 1; i >= 0; i--) {
                   int offset = i * 8; //24, 16, 8
                   intValue |= (bytes[i] & 0xFF) << offset;
                  }
                 return intValue;
          }
          /**
          * 操作符 << 的優先級比 & 高
          * longValue = (long)(bytes[7] & 0xFF) << 56
                  | (long)(bytes[6] & 0xFF) << 48
                  | (long)(bytes[5] & 0xFF) << 40
                  | (long)(bytes[4] & 0xFF) << 32
                  | (long)(bytes[3] & 0xFF) << 24
                  | (long)(bytes[2] & 0xFF) << 16
                  | (long)(bytes[1] & 0xFF) <<  8
                  | (long)(bytes[0] & 0xFF) <<  0
          * @param bytes
          * @return
          */
          public static long bytesToLong (byte[] bytes){
          int length = 8;
          long longValue = 0;
                  for (int i = length - 1; i >= 0; i--) {
                   int offset = i * 8; //56, 48, 40, 32, 24, 16, 8
                   longValue |= (long)(bytes[i] & 0xFF) << offset; //一定要先強制轉換成long型再移位, 因為0xFF為int型
                  }
                 return longValue;
          }
          public static float bytesToFloat(byte[] bytes) {
          return Float.intBitsToFloat(bytesToInt(bytes));
          }
          public static double bytesToDouble(byte[] bytes) {
          return Double.longBitsToDouble(bytesToLong(bytes));
          }
          public static byte[] floatToBytes(float value){
          return intToBytes(Float.floatToIntBits(value));
          }

          public static byte[] doubleToBytes(double value){
          return longToBytes(Double.doubleToLongBits(value));
          }
          }
          ====================================================================================
          /**
          public static String bytes2HexString(byte[] b) { 
          String ret = ""; 
          for (int i = 0; i < b.length; i++) { 
          String hex = Integer.toHexString(b[ i ] 0xFF); 
          if (hex.length() == 1) {
            hex = '0' + hex; 
           
            ret += hex.toUpperCase(); 
           
            return ret; 
           
           上面是將byte[]轉化十六進制的字符串,注意這里b[ i ] & 0xFF將一個byte和 0xFF進行了與運算,
           然后使用Integer.toHexString取得了十六進制字符串,可以看出 b[ i ] & 0xFF運算后得出的仍然
           是個int,那么為何要和 0xFF進行與運算呢?
           直接 Integer.toHexString(b[ i ]);,將byte強轉為int不行嗎?答案是不行的. 
           其原因在于: 
           1.byte的大小為8bits而int的大小為32bits 
           2.java的二進制采用的是補碼形式 
           
           在這里先溫習下計算機基礎理論 byte是一個字節保存的,有8個位,即8個0、1。 
           8位的第一個位是符號位, 也就是說
           0000 0001代表的是數字1 
           1000 0000代表的就是-1 
           所以
           正數最大為0111 1111,也就是數字127 
           負數最大為1111 1111,也就是數字-128 
           上面說的是二進制原碼,
           但是在java中采用的是補碼的形式,而不是原碼,
           
           下面介紹下什么是補碼 
           1、反碼:一個數如果是正,則它的反碼與原碼相同; 一個數如果是負,則符號位為1,其余各位是對原碼取反; 
           2、補碼:利用溢出,我們可以將減法變成加法,
            對于十進制數,從9得到5可用減法: 9-4=5。
            因為4+6=10,我們可以將6作為4的補數,改寫為加法: 9+6=15(去掉高位1,也就是減10)得到5. 
            對于十六進制數,從c到5可用減法: c-7=5。
            因為7+9=16 將9作為7的補數,改寫為加法: c+9=15(去掉高位1,也就是減16)得到5. 
           
           在計算機中,如果我們用1個字節表示一個數,一個字節有8位,超過8位就進1,在內存中情況為(1 0000 0000),進位1被丟棄。
           所以補碼是這樣運算的:
            ⑴一個數為正,則它的原碼、反碼、補碼相同
            ⑵一個數為負,高符號位為1,其余各位是對原碼取反,然后整個數加1,(符號位不變,其余為取反再加1)
            -1的原碼為 10000001 
            -1的反碼為 11111110 再 + 1 
            -1的補碼為 11111111 
            
            0的原碼為  00000000 
            0的反碼為  11111111 (正零和負零的反碼相同) 再 + 1
            0的補碼為 100000000(舍掉打頭的1,正零和負零的補碼相同) 
            
            Integer.toHexString的參數是int,如果不進行&0xff,那么當一個byte轉換成int時,
            由于int是32位,而byte只有8位,這時會進行補位,補位補的是1
            例如:補碼11111111的十進制數為-1,轉換為int時變為11111111 11111111 11111111 11111111
            好多1啊,呵呵!即 0xff ff ff ff ,但是這個數是不對的,
            這種補位就會造成誤差。和0xff相與后,高24bits就會被清0了,結果就對了。(任是補碼11111111)
          */
          主站蜘蛛池模板: 垣曲县| 荃湾区| 富源县| 信宜市| 伊通| 墨竹工卡县| 六安市| 改则县| 广元市| 额敏县| 津南区| 石林| 鲜城| 大姚县| 华阴市| 石城县| 木兰县| 台中县| 广南县| 谷城县| 河北区| 得荣县| 靖西县| 印江| 嘉禾县| 博客| 玉环县| 咸丰县| 南充市| 兰坪| 和平县| 大英县| 平顶山市| 安康市| 北宁市| 松滋市| 潮安县| 黎平县| 广州市| 潢川县| 即墨市|