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

          Posted on 2012-04-28 08:48 馬少 閱讀(111) 評論(0)  編輯  收藏

          intchardoublebyte相互轉(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) { //最后一個之所以不乘,是因?yàn)榭赡軙绯?span lang="EN-US">

          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;

          }

           

          //浮點(diǎn)到字節(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é)到浮點(diǎn)轉(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);

          }



          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           

          posts - 0, comments - 0, trackbacks - 0, articles - 2

          Copyright © 馬少

          版權(quán)所有 侵權(quán)必究
          主站蜘蛛池模板: 大埔县| 卓资县| 凤凰县| 内丘县| 珲春市| 阳山县| 称多县| 大余县| 黎川县| 成都市| 澄城县| 潼关县| 台北市| 贡觉县| 张家口市| 桐庐县| 舒兰市| 灵宝市| 丰城市| 砀山县| 久治县| 绵阳市| 禹城市| 图木舒克市| 天台县| 区。| 太谷县| 吴旗县| 从化市| 汉阴县| 仙居县| 小金县| 东海县| 梁平县| 石屏县| 鲜城| 高邑县| 永和县| 泰顺县| 霍林郭勒市| 乐至县|