java byte數(shù)組與16進制間相互轉(zhuǎn)換的示例
1.準(zhǔn)備工作
import java.util.Arrays;/** * Byte[]與hex的相互轉(zhuǎn)換 * @explain * @author Marydon * @creationTime 2018年6月11日下午2:29:11 * @version 1.0 * @since * @email [email protected] */public class ByteUtils { // 16進制字符 private static final char[] HEX_CHAR = { ’0’, ’1’, ’2’, ’3’, ’4’, ’5’, ’6’, ’7’, ’8’, ’9’, ’a’, ’b’, ’c’, ’d’, ’e’, ’f’ };}
2.byte類型數(shù)組轉(zhuǎn)化成16進制字符串
方法一
/** * 方法一:將byte類型數(shù)組轉(zhuǎn)化成16進制字符串 * @explain 字符串拼接 * @param bytes * @return */public static String toHexString(byte[] bytes) { StringBuilder sb = new StringBuilder(); int num; for (byte b : bytes) { num = b < 0 ? 256 + b : b; sb.append(HEX_CHAR[num / 16]).append(HEX_CHAR[num % 16]); } return sb.toString();}
方法二
/** * 方法二: byte[] to hex string * @explain 使用數(shù)組 * @param bytes * @return */public static String toHexString2(byte[] bytes) { // 一個byte為8位,可用兩個十六進制位表示 char[] buf = new char[bytes.length * 2]; int a = 0; int index = 0; // 使用除與取余進行轉(zhuǎn)換 for (byte b : bytes) { if (b < 0) a = 256 + b; else a = b; // 偶數(shù)位用商表示 buf[index++] = HEX_CHAR[a / 16]; // 奇數(shù)位用余數(shù)表示 buf[index++] = HEX_CHAR[a % 16]; } // char[]-->String return new String(buf);}
方法三
/** * 方法三: byte[]-->hexString * @explain 使用位運算 * @param bytes * @return */public static String toHexString3(byte[] bytes) { char[] buf = new char[bytes.length * 2]; int index = 0; // 利用位運算進行轉(zhuǎn)換,可以看作方法二的變型 for (byte b : bytes) { buf[index++] = HEX_CHAR[b >>> 4 & 0xf]; buf[index++] = HEX_CHAR[b & 0xf]; } return new String(buf);}
方法四
/** * 方法四:byte[]-->hexString * @param bytes * @return */public static String toHexString4(byte[] bytes) { StringBuilder sb = new StringBuilder(bytes.length * 2); // 使用String的format方法進行轉(zhuǎn)換 for (byte b : bytes) { sb.append(String.format('%02x', new Integer(b & 0xff))); } return sb.toString();}
方法五
/** * 將byte數(shù)組轉(zhuǎn)換成16進制字符串 * * @param src * @return */private static String bytesToHexString(byte[] src) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < src.length; i++) { int v = src[i] & 0xFF; String hv = Integer.toHexString(v); if (hv.length() < 2) { sb.append(0); } sb.append(hv); } return sb.toString();}
3.16進制字符串轉(zhuǎn)換為byte[]
方法一
/** * 將16進制字符串轉(zhuǎn)換為byte[] * @explain 16進制字符串不區(qū)分大小寫,返回的數(shù)組相同 * @param hexString * 16進制字符串 * @return byte[] */public static byte[] fromHexString(String hexString) { if (null == hexString || ''.equals(hexString.trim())) { return new byte[0]; } byte[] bytes = new byte[hexString.length() / 2]; // 16進制字符串 String hex; for (int i = 0; i < hexString.length() / 2; i++) { // 每次截取2位 hex = hexString.substring(i * 2, i * 2 + 2); // 16進制-->十進制 bytes[i] = (byte) Integer.parseInt(hex, 16); } return bytes;}
方法二
/** * 將16進制轉(zhuǎn)換為byte[] * @param hexStr * @return */public static byte[] fromHex(String hexStr) { if (hexStr.length() < 1) return null; byte[] result = new byte[hexStr.length() / 2]; for (int i = 0; i < hexStr.length() / 2; i++) { int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16); int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16); result[i] = (byte) (high * 16 + low); } return result;}
方法三:
public static byte[] toByteArray(String data) { if (data == null) {return new byte[] {}; } if (data.length() == 0) {return new byte[] {}; } while (data.length() < 2) {data = '0' + data; } if (data.substring(0, 2).toLowerCase().equals('0x')) {data = data.substring(2); } if (data.length() % 2 == 1) {data = '0' + data; } data = data.toUpperCase(); byte[] bytes = new byte[data.length() / 2]; String hexString = '0123456789ABCDEF'; for (int i = 0; i < bytes.length; i++) {int byteConv = hexString.indexOf(data.charAt(i * 2)) * 0x10;byteConv += hexString.indexOf(data.charAt(i * 2 + 1));bytes[i] = (byte) (byteConv & 0xFF); } return bytes;}
4.測試
public static void main(String[] args) throws Exception { String json = '{'name':'Marydon','website':'http://www.cnblogs.com/Marydon20170307'}'; byte[] bytes = json.getBytes('utf-8'); System.out.println('字節(jié)數(shù)組為:' + Arrays.toString(bytes)); System.out.println('byte數(shù)組轉(zhuǎn)16進制之方法一:' + toHexString(bytes)); System.out.println('byte數(shù)組轉(zhuǎn)16進制之方法二:' + ByteUtils.toHexString2(bytes)); System.out.println('byte數(shù)組轉(zhuǎn)16進制之方法三:' + ByteUtils.toHexString3(bytes)); System.out.println('byte數(shù)組轉(zhuǎn)16進制之方法四:' + ByteUtils.toHexString4(bytes)); System.out.println('=================================='); String str = '7b226e616d65223a224d617279646f6e222c2277656273697465223a22687474703a2f2f7777772e636e626c6f67732e636f6d2f4d617279646f6e3230313730333037227d'; System.out.println('轉(zhuǎn)換后的字節(jié)數(shù)組:' + Arrays.toString(fromHexString(str))); System.out.println(new String(fromHexString(str), 'utf-8'));}
補充
1B=8b,也就是1byte=8bit;
1KB=1024B;
1MB=1024KB;
1GB=1024MB;
1TB=1024GB
bit是計算機最小的存儲單元,只能存儲0和1,是Binary digit(二進制數(shù)位)的縮寫,意為“位”或“比特”,也就是二進制。
以上就是java byte數(shù)組與16進制間相互轉(zhuǎn)換的示例的詳細內(nèi)容,更多關(guān)于java byte數(shù)組與16進制間的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. JavaWeb Servlet中url-pattern的使用2. 淺談SpringMVC jsp前臺獲取參數(shù)的方式 EL表達式3. asp(vbscript)中自定義函數(shù)的默認參數(shù)實現(xiàn)代碼4. React優(yōu)雅的封裝SvgIcon組件示例5. 輕松學(xué)習(xí)XML教程6. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究7. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)8. jsp中sitemesh修改tagRule技術(shù)分享9. ASP基礎(chǔ)知識VBScript基本元素講解10. 詳解瀏覽器的緩存機制
