Created
December 14, 2021 06:10
-
-
Save 330132662/41bca3ce3b574e38c4ff3be97e946383 to your computer and use it in GitHub Desktop.
从File中读取文件十六进制内容,途中进行补位
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static final String readHexContent(String filePath) { | |
FileInputStream in = null; | |
try { | |
in = new FileInputStream(filePath); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} | |
int b = 0; | |
int i = 1; | |
StringBuilder builder = new StringBuilder(); | |
while (in != null)//b=in.read()这就是程序核心 | |
{ | |
try { | |
if ((b = in.read()) == -1) { | |
break; | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
String item = Integer.toHexString(b); | |
if (item.length() == 1) { | |
builder.append("0"); | |
} | |
builder.append(item); | |
} | |
try { | |
in.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return builder.toString().trim(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment