隨筆-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
          主站蜘蛛池模板: 三门县| 讷河市| 东宁县| 兴化市| 仙游县| 永定县| 龙门县| 神木县| 洞头县| 广安市| 汉阴县| 体育| 乌拉特前旗| 修水县| 湘潭县| 漳浦县| 龙游县| 定远县| 会昌县| 德化县| 新巴尔虎右旗| 永安市| 台中市| 武定县| 韶关市| 武义县| 米泉市| 昭通市| 连平县| 门源| 新津县| 乌鲁木齐市| 靖西县| 临漳县| 自治县| 彩票| 江西省| 漯河市| 兰考县| 郁南县| 察雅县|