隨筆-348  評論-598  文章-0  trackbacks-0
          無符號數(shù)都可以用BigInteger來完成表現(xiàn),他自身附帶了很多計(jì)算方法,可以有效地完成無符號數(shù)的計(jì)算工作。
          /**
               * 逆轉(zhuǎn)字節(jié)數(shù)組
               * 
               * 
          @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數(shù),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");
                  
          // 處理成無符號數(shù)
                  byte[] uint64 = new byte[3];
                  uint64[
          2= 0;
                  System.arraycopy(readBuffer, 
          0, uint64, 02);
                  
          return new BigInteger(reverse(uint64));
              }

              
          /**
               * 讀取無符號位的長整數(shù),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");
                  
          // 處理成無符號數(shù)
                  byte[] uint64 = new byte[9];
                  uint64[
          8= 0;
                  System.arraycopy(readBuffer, 
          0, uint64, 08);
                  
          return new BigInteger(reverse(uint64));
              }


          ---------------------------------------------------------
          專注移動(dòng)開發(fā)

          Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
          posted on 2010-05-03 09:18 TiGERTiAN 閱讀(9130) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 中阳县| 通化市| 且末县| 集贤县| 衡水市| 同仁县| 青阳县| 万宁市| 右玉县| 陆河县| 稷山县| 汪清县| 濉溪县| 紫金县| 泰宁县| 夏邑县| 东平县| 鄂托克旗| 海门市| 绥芬河市| 鞍山市| 怀柔区| 个旧市| 武城县| 临西县| 任丘市| 崇左市| 漠河县| 江口县| 苍南县| 安顺市| 阿巴嘎旗| 乐山市| 花莲县| 古蔺县| 迁安市| 宁武县| 集安市| 睢宁县| 射阳县| 喀喇沁旗|