使用Java將字節(jié)數(shù)組轉(zhuǎn)成16進(jìn)制形式的代碼實(shí)現(xiàn)
概述
在很多場景下,需要進(jìn)行分析字節(jié)數(shù)據(jù),但是我們存起來的字節(jié)數(shù)據(jù)一般都是二進(jìn)制的,這時候就需要我們將其轉(zhuǎn)成16進(jìn)制的方式方便分析。比如在做音視頻的時候,需要看下我們傳輸?shù)囊曨lh264數(shù)據(jù)中是否有對應(yīng)的I幀或者B幀等數(shù)據(jù),做ASM插樁的時候,可以使用輸出類結(jié)構(gòu)的16進(jìn)制輔助分析了解問題。測試投屏的時候尤其有用,比如說投屏到電視上后,發(fā)現(xiàn)沒有畫面,或者是畫面很卡頓,這時候就需要對我們傳輸?shù)囊曨l數(shù)據(jù)做分析,所以我們將視頻的數(shù)據(jù)轉(zhuǎn)成16進(jìn)制的形式,并且以一定的格式輸出,可以很方便的幫助我們定位問題。本文主要介紹如何使用Java將字節(jié)數(shù)組格式化成16進(jìn)制的格式并輸出。
輸出效果展示

上圖是以一個class字節(jié)碼文件的16進(jìn)制的格式輸出,下面就介紹如何將我們的字節(jié)數(shù)組輸出成16進(jìn)制的格式
代碼實(shí)現(xiàn)
首先我們定義一個類,用于生成一個class文件,作為我們格式化的對象。讀者使用的時候可以是其他數(shù)據(jù),只要是字節(jié)數(shù)組的方式提供就行了,這里僅僅作為演示
public class ASMDemoEntity {
private int intNum = 10;
private static final String staticString = "hello world";
public void fun() {
System.out.println("I am fun");
}
public int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
System.out.println("a+b = " + new ASMDemoEntity().add(1, 2));
new ASMDemoEntity().fun();
}
}
然后定義一個枚舉類,定義我們格式化后的16進(jìn)制數(shù)據(jù)的輸出樣式以及分隔符,如下所示:
public enum HexFormat {
// 無分隔符分別展示0,8,16,32列
FORMAT_HEX_0("", 0),
FORMAT_HEX_8("", 8),
FORMAT_HEX_16("", 16),
FORMAT_HEX_32("", 32),
// 帶空格分隔符分別展示0,8,16,32列
FORMAT_HEX_SPACE__0(" ", 0),
FORMAT_HEX_SPACE_8(" ", 8),
FORMAT_HEX_SPACE_16(" ", 16),
FORMAT_HEX_SPACE_32(" ", 32);
public final String separator; // 分隔符
public final int column; // 展示幾列
HexFormat(String separator, int column) {
this.separator = separator;
this.column = column;
}
}
如上所示:FORMAT_HEX_0就表示展示0列,無分隔符,用一行展示完所有的16進(jìn)制數(shù)據(jù),而FORMAT_HEX_SPACE_32 表示以一個空格做分隔符,展示32列,就如我們本文展示的效果圖一樣。 接著我們使用一個FileUtil類去讀我們生成的.class文件:
public class FileUtil {
public static String getFilePath(String relativePath){
URL resource = FileUtil.class.getResource("/");
String dir = resource == null? "" : resource.getPath();
return dir + relativePath;
}
public static byte[] readBytes(String filePath){
File file = new File(filePath);
if(!file.exists()){
throw new IllegalArgumentException(filePath + "not exist");
}
InputStream in = null;
try {
in = Files.newInputStream(file.toPath());
in = new BufferedInputStream(in);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
IOUtil.copy(in,bao);
return bao.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}finally {
IOUtil.closeIO(in);
}
return null;
}
}
使用一個IOUtil類做復(fù)制字節(jié)數(shù)組和關(guān)閉IO流
public class IOUtil {
private static final int EOF = -1;
private static final int BUFFER_SIZE = 1024 * 4;
public static long copy(final InputStream input,
final OutputStream output) throws IOException {
long count = 0;
int n;
byte[] buffer = new byte[BUFFER_SIZE];
while (EOF != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
public static void closeIO(final Closeable closeable) {
if(closeable != null){
try {
closeable.close();
} catch (IOException ignored) {
}
}
}
}
最后使用格式化工具類將字節(jié)數(shù)組格式化成16進(jìn)制的樣式并按照指定的格式輸出
public class HexUtil {
public static String hexFormat(byte[] bytes,HexFormat format){
String separator = format.separator;
int column = format.column;
return hexFormat(bytes,separator,column);
}
private static String hexFormat(byte[] bytes, String separator, int column) {
if(bytes == null || bytes.length < 1) {
return "";
}
StringBuilder sb = new StringBuilder();
Formatter fm = new Formatter(sb);
int length = bytes.length;
for (int i = 0; i < length; i++) {
int val = bytes[i] & 0xFF;
fm.format("%02X",val);
if(column > 0 && (i+1) % column == 0){
fm.format("%n");
}else{
fm.format("%s",separator);
}
}
return sb.toString();
}
}
在上面代碼中的代碼是Formatter.format()方法,它的作用是格式化我們的字節(jié)數(shù)組,我們傳入的格式中帶有%..X..時表示輸出16進(jìn)制數(shù)據(jù),具體的定義如下: %X: 正常輸出16進(jìn)制數(shù) %NX: 十六進(jìn)制數(shù),輸出N位,如果本身大于N位,正常輸出,比如
format("%2X",val);表示輸出2位16進(jìn)制數(shù),若本身大于2位,正常輸出 %NBX: 十六進(jìn)制數(shù),輸出N位,不足N位就補(bǔ)B,若本身大于N位,就正常輸出,比如format("%02X",val);代表輸出2位的16進(jìn)制數(shù),如果不足2位就補(bǔ)0,如果本身大于2位,就正常輸出
演示將一個class文件的二進(jìn)制數(shù)據(jù)轉(zhuǎn)成16進(jìn)制數(shù)據(jù)并格式化后輸出:
public class HexFormatMain {
public static void main(String[] args) {
String relativePath = "org/example/entity/ASMDemoEntity.class";
String filePath = FileUtil.getFilePath(relativePath);
System.out.println("file path: " + filePath);
byte[] bytes = FileUtil.readBytes(filePath);
String hex = HexUtil.hexFormat(bytes, HexFormat.FORMAT_HEX_SPACE_32);
System.out.println("class文件的16進(jìn)制: ");
System.out.println(hex);
}
}
到此這篇關(guān)于使用Java將字節(jié)數(shù)組轉(zhuǎn)成16進(jìn)制形式的代碼實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Java字節(jié)數(shù)組轉(zhuǎn)16進(jìn)制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring使用Filter過濾器對Response返回值進(jìn)行修改的方法
這篇文章主要介紹了spring使用Filter過濾器對Response返回值進(jìn)行修改,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09
JavaCV調(diào)用百度AI實(shí)現(xiàn)人臉檢測方法詳解
在檢測人臉數(shù)量、位置、性別、口罩等場景時,可以考慮使用百度開放平臺提供的web接口,一個web請求就能完成檢測得到結(jié)果。本文就為大家介紹JavaCV如何調(diào)用百度AI實(shí)現(xiàn)最簡單的人臉檢測,需要的可以參考一下2022-01-01
Java Socket實(shí)現(xiàn)文件傳輸示例代碼
這篇文章主要介紹了Java Socket實(shí)現(xiàn)文件傳輸示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01
springboot中Controller中postman遇到的傳參問題及解決
這篇文章主要介紹了springboot中Controller中postman遇到的傳參問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
Java設(shè)計模式之狀態(tài)模式(State模式)介紹
這篇文章主要介紹了Java設(shè)計模式之狀態(tài)模式(State模式)介紹,本文講解了何時使用狀態(tài)模式、如何使用狀態(tài)模式等內(nèi)容,需要的朋友可以參考下2015-03-03

