电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

int、char、double与byte相互转换的程序


发布日期:2021/3/21
 

intchardouble与byte相互转换的程序

//整数到字节数组的转换

public static byte[] intToByte(int number) {

int temp = number;

byte[] b=new byte[];

for (int i=blength;i>;i){

b[i] = new Integer(temp&xff)bytevalue();//将最高位保存在最低位

temp = temp >> ; //向右移

}

return b;

}

//字节数组到整数的转换

public static int byteToInt(byte[] b) {

int s = ;

for (int i = ; i < ; i++) {

if (b[i] >= )

s = s + b[i];

else

s = s + + b[i];

s = s * ;

}

if (b[] >= ) //最后一个之所以不乘是因为可能会溢出

s = s + b[];

else

s = s + + b[];

return s;

}

//字符到字节转换

public static byte[] charToByte(char ch){

int temp=(int)ch;

byte[] b=new byte[];

for (int i=blength;i>;i){

b[i] = new Integer(temp&xff)bytevalue();//将最高位保存在最低位

temp = temp >> ; //向右移

}

return b;

}

//字节到字符转换

public static char byteToChar(byte[] b){

int s=;

if(b[]>)

s+=b[];

else

s+=+b[];

s*=;

if(b[]>)

s+=b[];

else

s+=+b[];

char ch=(char)s;

return ch;

}

//浮点到字节转换

public static byte[] doubleToByte(double d){

byte[] b=new byte[];

long l=DoubledoubleToLongBits(d);

for(int i=;ilength;i++){

b[i]=new Long(l)bytevalue();

l=l>>;

}

return b;

}

//字节到浮点转换

public static double byteToDouble(byte[] b){

long l;

l=b[];

l&=xff;

l|=((long)b[]<<);

l&=xffff;

l|=((long)b[]<<);

l&=xffffff;

l|=((long)b[]<<);

l&=xffffffffl;

l|=((long)b[]<<);

l&=xffffffffffl;

l|=((long)b[]<<);

l&=xffffffffffffl;

l|=((long)b[]<<48);

l|=((long)b[7]<<56);

return Double.longBitsToDouble(l);

}

--

上一篇:从outlook导入email地址

下一篇:printscreen读取位图转为jpeg格式输出