今日,小弟我做题目遇到了Double型转换成十六进制的问题,到网上找了很多方法,结果都是手动的,最后,我一狠心,自己做了一个实验,代码如下:
import java.io.*;
public class HexChange
{
public static void main(String args[]) throws IOException
{
File file=new File("E:\\app.bin");/*创建一个二进制文件*/
FileOutputStream fos=new FileOutputStream(file);
DataOutputStream dos=new DataOutputStream(fos);/*创建一个字节流*/
double num=2.65;
Long numtolong=Double. doubleToLongBits(num);
Byte b1=numbertolong>>>0;
Byte b2=numbertolong>>>8;
Byte b3=numbertolong>>>16;
Byte b4=numbertolong>>>24;
Byte b5=numbertolong>>>32;
Byte b6=numbertolong>>>40;
Byte b7=numbertolong>>>48;
Byte b8=numbertolong>>>56;
/*Double占8个字节*/
dos.writeByte(b1);
dos.writeByte(b2);
dos.writeByte(b3);
dos.writeByte(b4);
dos.writeByte(b5);
dos.writeByte(b6);
dos.writeByte(b7);
dos.writeByte(b8);
dos.close();
fos.close();
}
}
然后你去app.bin里面看的就是2.56转换后的十六进制(记得要用Ultra Edit看这个十六进制文件哟)