int、char、double与byte相互转换的程序
来源:互联网 发布:php url跳转代码 编辑:程序博客网 时间:2024/05/17 02:09
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.length;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);
}
- int、char、double与byte相互转换的程序
- int、char、double与byte相互转换的程序
- int、char、double与byte相互转换的程序
- int、char、double与byte相互转换的程序
- int、char、double与byte相互转换的程序
- int、char、double与byte相互转换的程序
- int、char、double与byte相互转换的程序
- int、char、double与byte相互转换
- int、char、double与byte相互转换
- int、char、double与byte相互转换
- C++中int与char*、int与Byte、double与char*的相互装换
- int、char、与byte相互转换
- MFC double char int 与 CString 相互转换
- 3.1 double 与 char* 的相互转换
- Java中byte数组与short,int,char,long,float,double之间的转换
- Java中byte数组与short,int,char,long,float,double之间的转换
- int 与 byte[] 的相互转换
- int 与 byte[] 的相互转换
- PHP 解析xml(包含非英文字符)
- int、char、double与byte相互转换的程序
- Javascript实现网页中元素的拖拽
- PHP 怎样取得本地化时间
- 绿色护腕 (原文发表于2006-06-10 下午01:14:07 )
- int、char、double与byte相互转换的程序
- asp用户登录代码
- 笑话一则
- 《Windows程式设计》笔记1
- 计算机无法访问,您可能没有权限使用网络资源.请与这台服务器的管理员联系以查明您
- 摆脱迷茫,明天去工作了!
- Eclipse开发struts
- .net资源站点汇总
- Google内幕