隨筆-348  評(píng)論-598  文章-0  trackbacks-0
          開(kāi)發(fā)的時(shí)候發(fā)現(xiàn),C#寫(xiě)入的字節(jié)順序是從低到高(左低到右高),而Java的DataInputStream讀取的數(shù)據(jù)是從高到低(左高到右低),所以當(dāng)我們要用Java讀取C#生成的二進(jìn)制文件的時(shí)候,需要將DataInputStream里面的幾個(gè)方法重寫(xiě)或者寫(xiě)一些輔助方法,例如下面兩個(gè)函數(shù)是用來(lái)讀取C#寫(xiě)入的無(wú)符號(hào)Short型和無(wú)符號(hào)長(zhǎng)整型數(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;
              }

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

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


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

          Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
          posted on 2010-05-02 22:25 TiGERTiAN 閱讀(3175) 評(píng)論(1)  編輯  收藏 所屬分類: JavaDotNet

          評(píng)論:
          # re: C#的BinaryWriter和Java的DataInputStream之間的數(shù)據(jù)相互轉(zhuǎn)換 2010-05-04 11:37 | 麗可酷
          DataInputStream之間的數(shù)據(jù)相互轉(zhuǎn)換   回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 含山县| 广饶县| 义乌市| 许昌市| 翁牛特旗| 奉化市| 镇坪县| 葫芦岛市| 宜黄县| 崇信县| 莆田市| 夏邑县| 舞钢市| 南岸区| 抚顺县| 融水| 崇文区| 八宿县| 历史| 克什克腾旗| 西峡县| 铜鼓县| 怀来县| 贵州省| 临海市| 五家渠市| 稻城县| 许昌市| 昂仁县| 凌云县| 柳林县| 澄江县| 安图县| 鲁甸县| 翁牛特旗| 抚松县| 武汉市| 阳城县| 葫芦岛市| 济宁市| 雅安市|