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

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

int、char、double與byte如何相互轉換

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

  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);
  }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 上思县| 文安县| 塔河县| 昔阳县| 夏津县| 定西市| 同仁县| 巴南区| 新丰县| 古交市| 唐河县| 八宿县| 永修县| 开封县| 金堂县| 郓城县| 扶沟县| 合肥市| 龙井市| 瑞安市| 读书| 临泉县| 巨鹿县| 绥德县| 永修县| 桃江县| 郧西县| 贺州市| 弋阳县| 崇左市| 六枝特区| 芮城县| 鞍山市| 霍林郭勒市| 连州市| 汝阳县| 双峰县| 奇台县| 阜宁县| 察雅县| 云南省|