国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

int、char、double與byte相互轉換的程序

2019-11-18 13:18:08
字體:
來源:轉載
供稿:網友

  int、char、double與byte相互轉換的程序
  //整數到字節數組的轉換
   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;
   }
  
   //字節數組到整數的轉換
   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;
   }
  
   //字符到字節轉換
   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;
   }
  
   //字節到字符轉換
  
   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;
   }
  
   //浮點到字節轉換
   public static byte[] doubleToByte(double d){
    byte[] b=new byte[8];
    long l=Double.doubleToLongBits(d);
    for(int i=0;i     b[i]=new Long(l).bytevalue();
     l=l>>8;
  
    }
    return b;
   }
  
   //字節到浮點轉換
   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);
   }
  
  --

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁晋县| 张掖市| 精河县| 托里县| 泉州市| 夏邑县| 新津县| 平泉县| 常州市| 拉萨市| 荃湾区| 阳新县| 靖宇县| 政和县| 珲春市| 光泽县| 绵竹市| 普格县| 浠水县| 成安县| 孝感市| 榆社县| 遂宁市| 收藏| 大足县| 土默特右旗| 平昌县| 台南县| 共和县| 天水市| 唐海县| 新河县| 个旧市| 墨竹工卡县| 陇川县| 迁安市| 宁安市| 河南省| 武胜县| 万宁市| 巴南区|