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

上圖是以一個(gè)class字節(jié)碼文件的16進(jìn)制的格式輸出,下面就介紹如何將我們的字節(jié)數(shù)組輸出成16進(jìn)制的格式
代碼實(shí)現(xiàn)
首先我們定義一個(gè)類,用于生成一個(gè)class文件,作為我們格式化的對(duì)象。讀者使用的時(shí)候可以是其他數(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();
}
}
然后定義一個(gè)枚舉類,定義我們格式化后的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 表示以一個(gè)空格做分隔符,展示32列,就如我們本文展示的效果圖一樣。 接著我們使用一個(gè)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;
}
}
使用一個(gè)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..時(shí)表示輸出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位,就正常輸出
演示將一個(gè)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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)學(xué)生選課系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)學(xué)生選課系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
Spring創(chuàng)建Bean完成后執(zhí)行指定代碼的幾種實(shí)現(xiàn)方式
在實(shí)際開發(fā)中經(jīng)常會(huì)遇到在spring容器加載完某個(gè)bean之后,需要執(zhí)行一些業(yè)務(wù)代碼的場(chǎng)景,本文給大家介紹Spring創(chuàng)建Bean完成后執(zhí)行指定代碼的幾種實(shí)現(xiàn)方式,感興趣的朋友一起看看吧2024-01-01
Spring Boot Admin微服務(wù)應(yīng)用監(jiān)控的實(shí)現(xiàn)
這篇文章主要介紹了Spring Boot Admin微服務(wù)應(yīng)用監(jiān)控,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
DoytoQuery中的關(guān)聯(lián)查詢方案示例詳解
這篇文章主要為大家介紹了DoytoQuery中的關(guān)聯(lián)查詢方案示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
SpringBoot+Thymeleaf實(shí)現(xiàn)生成PDF文檔
Thymeleaf是一個(gè)現(xiàn)代的服務(wù)器端?Java?模板引擎,適用于?Web?和獨(dú)立環(huán)境。Thymeleaf?的主要目標(biāo)是為您的開發(fā)工作流程帶來優(yōu)雅的自然模板,本文就來用它實(shí)現(xiàn)生成PDF,感興趣的可以了解一下2022-09-09

