日期:2014-05-20  浏览次数:20820 次

如何将一个数据类型十六进制的文本文件转换成十进制的?
在D盘上,有一个16进制的文本文件:ts.txt

用UE查看是如下:
58 00 00 00 B8 1F 00 00 F1 54 C8 2A 1B DF F3 AA ;
A3 B0 CC 01 97 52 1A 00 40 18 B2 DE 14 B8 03 01 ;
... .....

想用java将这个文件中16进制的数据转换成十进制并存储到tz.txt文件中。
------解决方案--------------------
I hope the code below is what you want.
Could you just describe your requirement much detail, like :
we have a data file, which is binary:
58 00 00 00 B8 1F 00 00 F1 54 C8 2A 1B DF F3 AA
I want it to be:
.......

In fact the 16 radix data you see is just a way for representation, you might want to see the result with 10 radix, but the value is still kept the same, correct?


BufferedInputStream in = null;
BufferedOutputStream out = null;
try {
    in = new BufferedInputStream(new FileInputStream(FILENAME));
    out = new BufferedOutputStream(new FileOutputStream(FILEOUT));
    byte[] buff = new byte[60];
    int cnt = in.read(buff, 0, 60);
    for (int i =0; i < cnt; ++i) {
        String val = Byte.toString(buff[i]);
        out.write(Byte.valueOf(val, 10));
    }
} finally {
    if (null != in)
         in.close();
    if (null != out)
         out.close();
}

------解决方案--------------------
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;


public class Test_13 {

public static void main(String[] args) throws IOException {
File binFile = new File("D:\\tmp\\libbc-south-shared.so");
File outFile = new File("d:\\tmp\\out.txt");
FileChannel ch = new  FileInputStream(binFile).getChannel();
BufferedWriter out = new BufferedWriter(new FileWriter(outFile));
MappedByteBuffer buff = ch.map(MapMode.READ_ONLY, 0, binFile.length());

while(buff.remaining()>=4){
int data = buff.getInt();
out.write(String.valueOf(data));
out.write("\t");
}
out.flush();
ch.close();
out.close();
System.out.println(Integer.MAX_VALUE);
System.out.println(String.valueOf(Integer.MAX_VALUE).length());
}

}