作者a0tim (~TIM~)
看板java
标题[问题] 请问关於HEX档的读档
时间Sun Apr 28 14:07:26 2013
小弟我最近在学8051微处理机
写好的程式经过编译成.hex档後还要转成.bin档
但是课本附的.hex档转.bin档的程式在WIN7-x64无法执行
於是想用java自己写个转档程式
爬文後听说.hex档最好用
FileInputStream来读档
然而转换过程如下
public class HEX2BIN {
public static void main(String[] args){
File F1 = new File("C:\\000.hex");
File F2 = new File("C:\\123.bin");
String F1_S = "";
String S = "";
try {
F2.createNewFile();
BufferedReader BR = new BufferedReader(new InputStreamReader(new
FileInputStream(F1)));
while((S=BR.readLine()) != null){
S = S.substring(1);
//.hex档每行第一个字都是':'
F1_S += hexToBin(S);
}
BufferedWriter BW = new BufferedWriter(new FileWriter(F2));
BW.write(F1_S);
BW.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
public static String hexToBin(String hex){
String bin = "";
String binFragment = "";
int iHex;
hex = hex.trim();
hex = hex.replaceFirst("0x", "");
for(int i = 0; i < hex.length(); i++){
iHex = Integer.parseInt(""+hex.charAt(i),16);
binFragment = Integer.toBinaryString(iHex);
while(binFragment.length() < 4){
binFragment = "0" + binFragment;
}
bin += binFragment;
}
return bin;
}
}
转换出来的.bin档并不能如预期的执行
不知是否哪里出问题了
恳请大大解答一下,谢谢!
附上.hex档:
http://ppt.cc/wcb_
附上.bin档:
http://ppt.cc/0g1Z
PS.以上是课本附的程式转换的结果,不是我转的
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 123.192.152.111
※ 编辑: a0tim 来自: 123.192.152.111 (04/28 14:14)
※ 编辑: a0tim 来自: 123.192.152.111 (04/28 14:18)
1F:→ tkcn:我不懂你为什麽附自己的程式码 + 课本程式的转换结果....? 04/28 14:35
3F:→ tkcn:btw,课本程式转换可能是换行字元\r\n 的问题,拿掉 \r 试试 04/28 14:44
4F:推 PsMonkey:档案格式问题基本上跟实作语言无关,故暂时锁文 04/28 16:20
5F:→ Marquess:你附上的hex跟bin档在资料内容上不一致 05/01 11:33
6F:推 sbrhsieh:看到Reader/Writer,不必看其他,九成九是错的 05/01 16:33