yeafee@夜飛郎

          it's usually better to solve problems with simplicity and finesse rather than muscle.

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            33 隨筆 :: 3 文章 :: 2 評論 :: 0 Trackbacks
          ??1?public?class?TypeConvert?{
          ??2??????
          ??3?????/**
          ??4??????*?整數到字節數組的轉換
          ??5??????*/
          ??6?????public?static?byte[]?intToByte(int?number)?{
          ??7?????????
          ??8?????????int?temp?=?number;
          ??9?????????byte[]?b?=?new?byte[4];
          ?10?????????for?(int?i?=?b.length?-?1;?i?>?-1;?i--)?{
          ?11?????????????b[i]?=?new?Integer(temp?&?0xff).byteValue();?//?將最高位保存在最低位
          ?12?????????????temp?=?temp?>>?8;?//?向右移8位
          ?13?????????}
          ?14?????????return?b;
          ?15?????}
          ?16?
          ?17?????/**
          ?18??????*?字節數組到整數的轉換
          ?19??????*?@param?b
          ?20??????*?@return
          ?21??????*/
          ?22?????public?static?int?byteToInt(byte[]?b)?{
          ?23?????????int?s?=?0;
          ?24?????????for?(int?i?=?0;?i?<?3;?i++)?{
          ?25?????????????if?(b[i]?>=?0)
          ?26?????????????????s?=?s?+?b[i];
          ?27?????????????else
          ?28?
          ?29?????????????????s?=?s?+?256?+?b[i];
          ?30?????????????s?=?s?*?256;
          ?31?????????}
          ?32?????????if?(b[3]?>=?0)?//?最后一個之所以不乘,是因為可能會溢出
          ?33?????????????s?=?s?+?b[3];
          ?34?????????else
          ?35?????????????s?=?s?+?256?+?b[3];
          ?36?????????return?s;
          ?37?????}
          ?38?
          ?39?????/**
          ?40??????*?字符到字節轉換
          ?41??????*?@param?ch
          ?42??????*?@return
          ?43??????*/
          ?44?????public?static?byte[]?charToByte(char?ch)?{
          ?45?????????int?temp?=?(int)?ch;
          ?46?????????byte[]?b?=?new?byte[2];
          ?47?????????for?(int?i?=?b.length?-?1;?i?>?-1;?i--)?{
          ?48?????????????b[i]?=?new?Integer(temp?&?0xff).byteValue();?//?將最高位保存在最低位
          ?49?????????????temp?=?temp?>>?8;?//?向右移8位
          ?50?????????}
          ?51?????????return?b;
          ?52?????}
          ?53?
          ?54?????/**
          ?55??????*?字節到字符轉換
          ?56??????*?@param?b
          ?57??????*?@return
          ?58??????*/
          ?59?????public?static?char?byteToChar(byte[]?b)?{
          ?60?????????int?s?=?0;
          ?61?????????if?(b[0]?>?0)
          ?62?????????????s?+=?b[0];
          ?63?????????else
          ?64?????????????s?+=?256?+?b[0];
          ?65?????????s?*=?256;
          ?66?????????if?(b[1]?>?0)
          ?67?????????????s?+=?b[1];
          ?68?????????else
          ?69?????????????s?+=?256?+?b[1];
          ?70?????????char?ch?=?(char)?s;
          ?71?????????return?ch;
          ?72?????}
          ?73?
          ?74?????/**
          ?75??????*?浮點到字節轉換
          ?76??????*?@param?d
          ?77??????*?@return
          ?78??????*/
          ?79?????public?static?byte[]?doubleToByte(double?d)?{
          ?80?????????byte[]?b?=?new?byte[8];
          ?81?????????long?l?=?Double.doubleToLongBits(d);
          ?82?????????for?(int?i?=?0;?i?<?b.length;?i++)?{
          ?83?????????????b[i]?=?new?Long(l).byteValue();
          ?84?????????????l?=?l?>>?8;
          ?85?
          ?86?????????}
          ?87?????????return?b;
          ?88?????}
          ?89?
          ?90?????/**
          ?91??????*?字節到浮點轉換
          ?92??????*?@param?b
          ?93??????*?@return
          ?94??????*/
          ?95?????public?static?double?byteToDouble(byte[]?b)?{
          ?96?????????long?l;
          ?97?
          ?98?????????l?=?b[0];
          ?99?????????l?&=?0xff;
          100?????????l?|=?((long)?b[1]?<<?8);
          101?????????l?&=?0xffff;
          102?????????l?|=?((long)?b[2]?<<?16);
          103?????????l?&=?0xffffff;
          104?????????l?|=?((long)?b[3]?<<?24);
          105?????????l?&=?0xffffffffl;
          106?????????l?|=?((long)?b[4]?<<?32);
          107?????????l?&=?0xffffffffffl;
          108?
          109?????????l?|=?((long)?b[5]?<<?40);
          110?????????l?&=?0xffffffffffffl;
          111?????????l?|=?((long)?b[6]?<<?48);
          112?
          113?????????l?|=?((long)?b[7]?<<?56);
          114?????????return?Double.longBitsToDouble(l);
          115?????}
          116?}
          posted on 2007-04-16 16:32 @yeafee 閱讀(444) 評論(0)  編輯  收藏 所屬分類: 程序語言
          主站蜘蛛池模板: 延庆县| 清水河县| 垦利县| 洪江市| 玛沁县| 潜江市| 阿城市| 庆云县| 太白县| 沁阳市| 上犹县| 施甸县| 漳州市| 台州市| 南宁市| 遂宁市| 鄄城县| 新沂市| 秦皇岛市| 类乌齐县| 海晏县| 和林格尔县| 商水县| 扎赉特旗| 鄂托克旗| 吉木萨尔县| 江达县| 新郑市| 三江| 利津县| 阜南县| 成都市| 门头沟区| 凤台县| 日土县| 河曲县| 岳池县| 余干县| 泗水县| 罗甸县| 精河县|