隨筆-348  評論-598  文章-0  trackbacks-0
          無符號數都可以用BigInteger來完成表現,他自身附帶了很多計算方法,可以有效地完成無符號數的計算工作。
          /**
               * 逆轉字節數組
               * 
               * 
          @param b
               * 
          @return
               
          */
              
          private static byte[] reverse(byte[] b) {

                  
          byte[] temp = new byte[b.length];
                  
          for (int i = 0; i < b.length; i++) {
                      temp[i] 
          = b[b.length - 1 - i];
                  }
                  
          return temp;
              }

              
          /**
               * 讀取無符號位的Short數,16位
               * 
               * 
          @param readBuffer
               * 
          @return
               * 
          @throws IOException
               
          */
              
          private static final BigInteger readUnsignedShort(byte[] readBuffer)
                      
          throws IOException {
                  
          if (readBuffer == null || readBuffer.length < 2)
                      
          return new BigInteger("0");
                  
          // 處理成無符號數
                  byte[] uint64 = new byte[3];
                  uint64[
          2= 0;
                  System.arraycopy(readBuffer, 
          0, uint64, 02);
                  
          return new BigInteger(reverse(uint64));
              }

              
          /**
               * 讀取無符號位的長整數,64位
               * 
               * 
          @param readBuffer
               * 
          @return
               * 
          @throws IOException
               
          */
              
          private static final BigInteger readUnsignedInt64(byte[] readBuffer)
                      
          throws IOException {
                  
          if (readBuffer == null || readBuffer.length < 8)
                      
          return new BigInteger("0");
                  
          // 處理成無符號數
                  byte[] uint64 = new byte[9];
                  uint64[
          8= 0;
                  System.arraycopy(readBuffer, 
          0, uint64, 08);
                  
          return new BigInteger(reverse(uint64));
              }


          ---------------------------------------------------------
          專注移動開發

          Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
          posted on 2010-05-03 09:18 TiGERTiAN 閱讀(9127) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 称多县| 锦屏县| 宽甸| 呼伦贝尔市| 宁海县| 冀州市| 客服| 车险| 和龙市| 保康县| 德钦县| 巍山| 深水埗区| 闽侯县| 界首市| 营口市| 乌拉特后旗| 柏乡县| 康平县| 宣化县| 永昌县| 剑河县| 怀宁县| 集贤县| 合江县| 文水县| 河间市| 宁明县| 嵊州市| 隆昌县| SHOW| 屯昌县| 刚察县| 通化市| 大邑县| 蓝田县| 宜兰市| 河东区| 高淳县| 南京市| 正阳县|