隨筆-348  評論-598  文章-0  trackbacks-0
          開發的時候發現,C#寫入的字節順序是從低到高(左低到右高),而Java的DataInputStream讀取的數據是從高到低(左高到右低),所以當我們要用Java讀取C#生成的二進制文件的時候,需要將DataInputStream里面的幾個方法重寫或者寫一些輔助方法,例如下面兩個函數是用來讀取C#寫入的無符號Short型和無符號長整型數據。 
          /**
               * 逆轉字節數組
               * 
               * 
          @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
               
          */
              
          public 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
               
          */
              
          public 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-02 22:25 TiGERTiAN 閱讀(3174) 評論(1)  編輯  收藏 所屬分類: JavaDotNet

          評論:
          # re: C#的BinaryWriter和Java的DataInputStream之間的數據相互轉換 2010-05-04 11:37 | 麗可酷
          DataInputStream之間的數據相互轉換   回復  更多評論
            
          專注移動開發--Windows Mobile, Android, iPhone, J2ME, BlackBerry, Symbian, Windows Phone

          慢慢混,慢慢學
          <2010年5月>
          2526272829301
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          常用鏈接

          留言簿(43)

          隨筆分類(402)

          隨筆檔案(306)

          相冊

          我的好友們

          搜索

          •  

          積分與排名

          • 積分 - 812546
          • 排名 - 50

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 乡宁县| 阿尔山市| 石棉县| 吉林市| 绥棱县| 武宣县| 津市市| 阿图什市| 青阳县| 德格县| 清水县| 嘉兴市| 吉水县| 长岛县| 明溪县| 宜都市| 惠来县| 道孚县| 苏州市| 北辰区| 萨嘎县| 建水县| 佛冈县| 宁海县| 乌鲁木齐市| 嵊泗县| 汾阳市| 荣昌县| 宜昌市| 交城县| 郴州市| 格尔木市| 怀集县| 普兰店市| 桃园市| 浙江省| 武宣县| 曲靖市| 大同市| 芜湖市| 乐陵市|