- File file = new File(filepath);
- DataInputStream dps = new DataInputStream(new FileInputStream(file));
- StringBuilder hexData = new StringBuilder();
- byte bt = 0;
- for(int i=0;i<file.length();i++) {
- bt = dps.readByte();
- // 以十六进制的无符号整数形式返回一个字符串表示形式。
- String str = Integer.toHexString(bt);
- if(str.length() == 8) { //去掉补位的f
- str = str.substring(6);
- }
- if(str.length() == 1) {
- str = "0"+str;
- }
- hexData.append(str.toUpperCase());
- }
- return hexData.toString();
- }
-
-
- public static void main(String[] args) throws IOException {
- String filepath = "E:\\a.txt";
- String s =dataInputStream(filepath);
- FileOutputStream fps = new FileOutputStream("E:\\b.txt");//将十六进制数存成文本
- fps.write(s.getBytes());
- fps.close();
- }
复制代码 |