1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
| import java.io.*; import java.nio.file.Files;
public class TestHex {
public static byte[] fileToByte(String filePath) throws IOException { byte[] bFile = Files.readAllBytes(new File(filePath).toPath()); return bFile; }
public static byte[] toBytes(String str) { if(str == null || str.trim().equals("")) { return new byte[0]; }
byte[] bytes = new byte[str.length() / 2]; for(int i = 0; i < str.length() / 2; i++) { String subStr = str.substring(i * 2, i * 2 + 2); subStr=xor(subStr,"05"); bytes[i] = (byte) Integer.parseInt(subStr, 16); }
return bytes; }
public static void writerFile(String str) { BufferedWriter bw = null; try { File file = new File("C:\\Users\\qqq\\Desktop\\新建文件夹\\test.txt"); if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file); bw = new BufferedWriter(fw); bw.write(""); bw.write(str); System.out.println("写入成功"); } catch (IOException ioe) { ioe.printStackTrace(); } finally { try { if(bw != null){ bw.close();} } catch (Exception ex) { System.out.println("Error in closing the BufferedWriter" + ex); } } }
private static String xor(String strHex_X,String strHex_Y){ String anotherBinary=Integer.toBinaryString(Integer.valueOf(strHex_X,16)); String thisBinary=Integer.toBinaryString(Integer.valueOf(strHex_Y,16)); String result = ""; if(anotherBinary.length() != 8){ for (int i = anotherBinary.length(); i <8; i++) { anotherBinary = "0"+anotherBinary; } } if(thisBinary.length() != 8){ for (int i = thisBinary.length(); i <8; i++) { thisBinary = "0"+thisBinary; } } for(int i=0;i<anotherBinary.length();i++){ if(thisBinary.charAt(i)==anotherBinary.charAt(i)) { result += "0"; }else{ result+="1"; } } return Integer.toHexString(Integer.parseInt(result, 2)); }
public static void getFileByBytes(byte[] bytes, String filePath, String fileName) { BufferedOutputStream bos = null; FileOutputStream fos = null; File file = null; try { File dir = new File(filePath); if (!dir.exists() && dir.isDirectory()) { dir.mkdirs(); } file = new File(filePath + "\\" + fileName); fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); bos.write(bytes); } catch (Exception e) { e.printStackTrace(); } finally { if (bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
public static String bytesToHexFun3(byte[] bytes) { StringBuilder buf = new StringBuilder(bytes.length * 2); for(byte b : bytes) { buf.append(String.format("%02x", new Integer(b & 0xff))); } return buf.toString(); }
public static void vpyToMP4(String beforeFilePath,String filePath,String fileName) throws Exception { byte[] arr = fileToByte(beforeFilePath); String hexStr = bytesToHexFun3(arr); hexStr = hexStr.substring(hexStr.indexOf("050505"), hexStr.length()); byte[] bytes = toBytes(hexStr); getFileByBytes(bytes, filePath, fileName); System.out.println(fileName+"-----转换完成"); }
}
|