codefans

          導(dǎo)航

          <2005年11月>
          303112345
          6789101112
          13141516171819
          20212223242526
          27282930123
          45678910

          統(tǒng)計

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          程序設(shè)計鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          int、char、double與byte相互轉(zhuǎn)換的程序

          轉(zhuǎn)載  http://www.matrix.org.cn/thread.shtml?forum_id=19&view_id=919
          int、char、double與byte相互轉(zhuǎn)換的程序
          //整數(shù)到字節(jié)數(shù)組的轉(zhuǎn)換
            public static byte[] intToByte(int number) {
              int temp = number;
              byte[] b=new byte[4];
              for (int i=b.length-1;i>-1;i--){
                b[i] = new Integer(temp&0xff).byteValue();      //將最高位保存在最低位
                temp = temp >> 8;       //向右移8位
              }
              return b;
            }

            //字節(jié)數(shù)組到整數(shù)的轉(zhuǎn)換
            public static int byteToInt(byte[] b) {
              int s = 0;
              for (int i = 0; i < 3; i++) {
                if (b[i] >= 0)
                  s = s + b[i];
                else


                  s = s + 256 + b[i];
                s = s * 256;
              }
              if (b[3] >= 0)       //最后一個之所以不乘,是因為可能會溢出
                s = s + b[3];
              else
                s = s + 256 + b[3];
              return s;
            }

            //字符到字節(jié)轉(zhuǎn)換
            public static byte[] charToByte(char ch){
              int temp=(int)ch;
              byte[] b=new byte[2];
              for (int i=b.length-1;i>-1;i--){
                b[i] = new Integer(temp&0xff).byteValue();      //將最高位保存在最低位
                temp = temp >> 8;       //向右移8位
              }
              return b;
            }

            //字節(jié)到字符轉(zhuǎn)換


            public static char byteToChar(byte[] b){
              int s=0;
              if(b[0]>0)
                s+=b[0];
              else
                s+=256+b[0];
              s*=256;
              if(b[1]>0)
                s+=b[1];
              else
                s+=256+b[1];
              char ch=(char)s;
              return ch;
            }

            //浮點到字節(jié)轉(zhuǎn)換
            public static byte[] doubleToByte(double d){
              byte[] b=new byte[8];
              long l=Double.doubleToLongBits(d);
              for(int i=0;i<b.length;i++){
                b[i]=new Long(l).byteValue();
                l=l>>8;


              }
              return b;
            }

            //字節(jié)到浮點轉(zhuǎn)換
            public static double byteToDouble(byte[] b){
              long l;

              l=b[0];
              l&=0xff;
              l|=((long)b[1]<<8);
              l&=0xffff;
              l|=((long)b[2]<<16);
              l&=0xffffff;
              l|=((long)b[3]<<24);
              l&=0xffffffffl;
              l|=((long)b[4]<<32);
              l&=0xffffffffffl;

              l|=((long)b[5]<<40);
              l&=0xffffffffffffl;
              l|=((long)b[6]<<48);


              l|=((long)b[7]<<56);
              return Double.longBitsToDouble(l);
            }

          --

          posted on 2005-11-25 12:42 春雷的博客 閱讀(922) 評論(0)  編輯  收藏 所屬分類: 技術(shù)

          主站蜘蛛池模板: 台东市| 天全县| 龙州县| 亚东县| 靖宇县| 阿克陶县| 遵义市| 海淀区| 封丘县| 奉节县| 乌拉特前旗| 新巴尔虎右旗| 龙山县| 肥乡县| 农安县| 社会| 滦南县| 广河县| 凤山市| 元阳县| 永寿县| 金溪县| 肇东市| 黄大仙区| 南充市| 钟祥市| 广平县| 榕江县| 梅州市| SHOW| 邻水| 朝阳市| 沂源县| 化德县| 钟山县| 区。| 辽阳县| 永城市| 从化市| 孝昌县| 昭觉县|