Skip to content

Commit 7e3d92e

Browse files
committed
test: 自定义pipe新增将字节转换为八进制字符串和十六进制字符串方法
1 parent 74100d2 commit 7e3d92e

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/test/java/top/meethigher/proxy/Pipe.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,29 @@ private WriteException(Throwable cause) {
174174
super(cause, true);
175175
}
176176
}
177+
178+
/**
179+
* 转换为八进制字符串
180+
* Wireshark默认抓包时,使用的就是八进制字符串
181+
*/
182+
private static String toOctString(byte[] buf) {
183+
StringBuilder sb = new StringBuilder();
184+
for (byte value : buf) {
185+
int b = value & 0xFF;// 转为无符号字节
186+
sb.append(String.format("%03o ", b));// ddd格式。每个d表示一个0-7的数字
187+
}
188+
return sb.toString();
189+
}
190+
191+
/**
192+
* 转换为16进制字符串
193+
*/
194+
private static String toHexString(byte[] buf) {
195+
StringBuilder sb = new StringBuilder();
196+
for (byte value : buf) {
197+
int b = value & 0xFF;//转为无符号字节
198+
sb.append(String.format("%02x ", b));// 两位十六进制,不足补0
199+
}
200+
return sb.toString();
201+
}
177202
}

0 commit comments

Comments
 (0)