Skip to content

Instantly share code, notes, and snippets.

@330132662
Created December 14, 2021 06:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 330132662/41bca3ce3b574e38c4ff3be97e946383 to your computer and use it in GitHub Desktop.
Save 330132662/41bca3ce3b574e38c4ff3be97e946383 to your computer and use it in GitHub Desktop.
从File中读取文件十六进制内容,途中进行补位
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