- UID
- 126691
- 帖子
- 39
- 主題
- 17
- 精華
- 0
- 積分
- 15
- 楓幣
- 852
- 威望
- 15
- 存款
- 0
- 贊助金額
- 0
- 推廣
- 0
- GP
- 16
- 閱讀權限
- 10
- 性別
- 保密
- 在線時間
- 7 小時
- 註冊時間
- 2016-1-20
- 最後登入
- 2023-6-4
|
16進製字符串:
00000018A0010098C68E00989A690000000000BC614E000055AA55AA
如何將上述16進製字符串轉換成如下:
0x00 0x00 ...
HeapBuffer[pos=0 lim=28 cap=2048: 00 00 00 18 A0 01 00 98 C6 8E 00 98 9A 69 00 00...]
public class T {
public static void main(String[] args) {
String s = "00000018A0010098C68E00989A690000000000BC614E000055AA55AA";
System.out.println(s);
byte[] b = HexString2Bytes(s);
System.out.println(Bytes2HexString(b));
}
/**
* 将指定byte数组以16进制的形式打印到控制台
*
* @param hint
* String
* @param b
* byte[]
* @return void
*/
public static void printHexString(String hint, byte[] b) {
System.out.print(hint);
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
System.out.print(hex.toUpperCase() + " ");
}
System.out.println("");
}
/**
*
* @param b
* byte[]
* @return String
*/
public static String Bytes2HexString(byte[] b) {
String ret = "";
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
ret += " 0x" + hex.toUpperCase();
}
return ret;
}
/**
* 将两个ASCII字符合成一个字节; 如:"EF"–> 0xEF
*
* @param src0
* byte
* @param src1
* byte
* @return byte
*/
public static byte uniteBytes(byte src0, byte src1) {
byte _b0 = Byte.decode("0x" + new String(new byte[] {src0})).byteValue();
_b0 = (byte) (_b0 << 4);
byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 })).byteValue();
byte ret = (byte) (_b0 ^ _b1);
return ret;
}
/**
* 将指定字符串src,以每两个字符分割转换为16进制形式 如:"2B44EFD9" –> byte[]{0x2B, 0×44, 0xEF,
* 0xD9}
*
* @param src
* String
* @return byte[]
*/
public static byte[] HexString2Bytes(String src) {
if (null == src || 0 == src.length()) {
return null;
}
byte[] ret = new byte[src.length() / 2];
byte[] tmp = src.getBytes();
for (int i = 0; i < (tmp.length / 2); i++) {
ret = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
}
return ret;
}
}
|
[發帖際遇]: a29343539 幫助「歐陽妮妮」在「臺藝大夜間部」考期末考,因運氣不佳遭人爆料,損失獎金 1 楓幣 |
幸運榜 / 衰神榜 |
|