posts - 6,comments - 49,trackbacks - 0

          新手上路,最好的見(jiàn)面禮莫過(guò)于資源分享了。

          畢業(yè)設(shè)計(jì)中關(guān)鍵技術(shù)是數(shù)據(jù)壓縮模塊,數(shù)據(jù)傳輸?shù)氖亲止?jié)數(shù)組,卻沒(méi)有發(fā)現(xiàn)Java庫(kù)中現(xiàn)成的整型于字節(jié)數(shù)組的轉(zhuǎn)換,雖然Integer包裝類(lèi)有toBinaryString()之類(lèi)的靜態(tài)方法使用,但是卻無(wú)法直接滿(mǎn)足要求一個(gè)整數(shù)與4個(gè)字節(jié)數(shù)組的對(duì)應(yīng),幸好String類(lèi)有g(shù)etBytes()幾個(gè)重載方法使用,要不然真的就哭死了

          記得net中就有BinConverter可以直接使用,而且實(shí)現(xiàn)了bytes與int16,int32,char等等多類(lèi)型的轉(zhuǎn)換,有點(diǎn)感慨,還是.net方便,呵呵

          也許是偶還沒(méi)有發(fā)現(xiàn)相關(guān)的Java庫(kù),google得也不深入,沒(méi)辦法,時(shí)間不等人,只好自己實(shí)現(xiàn)了一個(gè),經(jīng)測(cè)試,正負(fù)數(shù)都OK(PS:google到一個(gè)算法,比較搞笑,只有負(fù)數(shù)是正確

          ?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版本,有時(shí)間了再研究其他數(shù)據(jù)類(lèi)型的了。

          歡迎大家拋磚,期待更加便捷的算法。
          posted on 2006-05-19 04:43 jinglecat 閱讀(1297) 評(píng)論(3)  編輯  收藏 所屬分類(lèi): 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?  回復(fù)  更多評(píng)論
            
          # re: int to bytes to int

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 郎溪县| 卢龙县| 嵊州市| 弥勒县| 安吉县| 中超| 武冈市| 江达县| 太康县| 保德县| 利津县| 定西市| 汾西县| 东至县| 莆田市| 西宁市| 青海省| 深圳市| 枝江市| 西华县| 蒙城县| 鲁山县| 绥化市| 凌源市| 娄烦县| 屏边| 静宁县| 咸阳市| 井冈山市| 东港市| 华蓥市| 扎鲁特旗| 眉山市| 浙江省| 和田县| 遂昌县| 车险| 开化县| 永新县| 麻栗坡县| 柏乡县|