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