萍聚社区-德国热线-德国实用信息网

 找回密码
 注册

微信登录

微信扫一扫,快速登录

萍聚头条

查看: 600|回复: 0

1-1-12-3 int、char、double与byte相互转换的程序,^_^

[复制链接]
发表于 2003-2-7 01:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册 微信登录

x

  1. 发信人: zhangnan (火警119), 信区: Java      
  2. 标  题: int、char、double与byte相互转换的程序,^_^
  3. 发信站: BBS 水木清华站 (Sat Mar 23 23:14:02 2002)

  4. //整数到字节数组的转换
  5.   public static byte[] intToByte(int number) {
  6.     int temp = number;
  7.     byte[] b=new byte[4];
  8.     for (int i=b.length-1;i>-1;i--){
  9.       b[i] = new Integer(temp&0xff).byteValue();      //将最高位保存在最低位
  10.       temp = temp >> 8;       //向右移8位
  11.     }
  12.     return b;
  13.   }

  14.   //字节数组到整数的转换
  15.   public static int byteToInt(byte[] b) {
  16.     int s = 0;
  17.     for (int i = 0; i < 3; i++) {
  18.       if (b[i] >= 0)
  19.         s = s + b[i];
  20.       else
  21.         s = s + 256 + b[i];
  22.       s = s * 256;
  23.     }
  24.     if (b[3] >= 0)       //最后一个之所以不乘,是因为可能会溢出
  25.       s = s + b[3];
  26.     else
  27.       s = s + 256 + b[3];
  28.     return s;
  29.   }

  30.   //字符到字节转换
  31.   public static byte[] charToByte(char ch){
  32.     int temp=(int)ch;
  33.     byte[] b=new byte[2];
  34.     for (int i=b.length-1;i>-1;i--){
  35.       b[i] = new Integer(temp&0xff).byteValue();      //将最高位保存在最低位
  36.       temp = temp >> 8;       //向右移8位
  37.     }
  38.     return b;
  39.   }

  40.   //字节到字符转换
  41.   public static char byteToChar(byte[] b){
  42.     int s=0;
  43.     if(b[0]>0)
  44.       s+=b[0];
  45.     else
  46.       s+=256+b[0];
  47.     s*=256;
  48.     if(b[1]>0)
  49.       s+=b[1];
  50.     else
  51.       s+=256+b[1];
  52.     char ch=(char)s;
  53.     return ch;
  54.   }

  55.   //浮点到字节转换
  56.   public static byte[] doubleToByte(double d){
  57.     byte[] b=new byte[8];
  58.     long l=Double.doubleToLongBits(d);
  59.     for(int i=0;i<b.length;i++){
  60.       b[i]=new Long(l).byteValue();
  61.       l=l>>8;
  62.     }
  63.     return b;
  64.   }

  65.   //字节到浮点转换
  66.   public static double byteToDouble(byte[] b){
  67.     long l;

  68.     l=b[0];
  69.     l&=0xff;
  70.     l|=((long)b[1]<<8);
  71.     l&=0xffff;
  72.     l|=((long)b[2]<<16);
  73.     l&=0xffffff;
  74.     l|=((long)b[3]<<24);
  75.     l&=0xffffffffl;
  76.     l|=((long)b[4]<<32);
  77.     l&=0xffffffffffl;

  78.     l|=((long)b[5]<<40);
  79.     l&=0xffffffffffffl;
  80.     l|=((long)b[6]<<48);
  81.     l&=0xffffffffffffffl;
  82.     l|=((long)b[7]<<56);
  83.     return Double.longBitsToDouble(l);
  84.   }

  85. --
复制代码
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
您需要登录后才可以回帖 登录 | 注册 微信登录

本版积分规则

手机版|Archiver|AGB|Impressum|Datenschutzerklärung|萍聚社区-德国热线-德国实用信息网 |网站地图

GMT+2, 2024-5-22 03:42 , Processed in 0.058777 second(s), 19 queries , MemCached On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表