曉風殘月
          新手上路
          posts - 6,comments - 49,trackbacks - 0

          新手上路,最好的見面禮莫過于資源分享了。

          畢業設計中關鍵技術是數據壓縮模塊,數據傳輸的是字節數組,卻沒有發現Java庫中現成的整型于字節數組的轉換,雖然Integer包裝類有toBinaryString()之類的靜態方法使用,但是卻無法直接滿足要求一個整數與4個字節數組的對應,幸好String類有getBytes()幾個重載方法使用,要不然真的就哭死了

          記得net中就有BinConverter可以直接使用,而且實現了bytes與int16,int32,char等等多類型的轉換,有點感慨,還是.net方便,呵呵

          也許是偶還沒有發現相關的Java庫,google得也不深入,沒辦法,時間不等人,只好自己實現了一個,經測試,正負數都OK(PS:google到一個算法,比較搞笑,只有負數是正確

          ?1 public ? class ?BitConverter? {
          ?2 ????
          ?3 ???? public ? static ? byte []?getBytes( int ?value)
          ?4 ???? {?
          ?5 ???????? byte []?bytes? = ? new ? byte [ 4 ];
          ?6 ????????bytes[ 0 ]? = ( byte )(?value? >> ? 24 ?);
          ?7 ????????bytes[ 1 ]? = ( byte )(?(value? << ? 8 )? >> ? 24 ?);
          ?8 ????????bytes[ 2 ]? = ( byte )(?(value? << ? 16 )? >> ? 24 ?);
          ?9 ????????bytes[ 3 ]? = ( byte )(?(value? << ? 24 )? >> ? 24 ?);????????
          10 ???????? return ?bytes;????????????
          11 ????}
          ?????????
          12
          13 ???? public ? static ? int ?toInt( byte []?bytes,? int ?startIndex) {????????
          14 ???????? int ?value? = ? 0 ;
          15 ???????? for ?( int ?j = startIndex;j < 4 ;j ++ )? {????????????
          16 ????????????value? = ?(value? << ? 8 )? | ?(bytes[j]? & ? 0xFF );
          17 ????????}
          ????????
          18 ???????? return ?value;????
          19 ????}

          20 }

          只有int版本,有時間了再研究其他數據類型的了。

          歡迎大家拋磚,期待更加便捷的算法。
          posted on 2006-05-19 04:43 jinglecat 閱讀(1294) 評論(3)  編輯  收藏 所屬分類: Java Prime

          FeedBack:
          # re: int to bytes to int
          # re: int to bytes to int
          2006-05-20 14:15 | jinglecat
          @Dedian
          thx so much!the url is appreciated.
          it's algorithm for "int to bytes" seems simpler than mine.
          it's a pity that it does not provider the implement of "int, float ... to bytes".
          Do others have any ideas?  回復  更多評論
            
          主站蜘蛛池模板: 广昌县| 定南县| 青浦区| 文成县| 鄂托克旗| 缙云县| 通江县| 綦江县| 郑州市| 广德县| 黔西| 怀化市| 呼伦贝尔市| 冷水江市| 绍兴市| 邯郸县| 会同县| 和龙市| 金阳县| 交城县| 慈溪市| 宜宾市| 宣化县| 永安市| 石门县| 沙田区| 怀远县| 新蔡县| 大新县| 墨玉县| 铜川市| 玛纳斯县| 台东县| 丹凤县| 永胜县| 凤山县| 五莲县| 乡宁县| 福贡县| 孝感市| 遂川县|