亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

java byte數(shù)組與16進制間相互轉(zhuǎn)換的示例

瀏覽:19日期:2022-08-22 09:51:33

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)文章!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 99久久99 | 色日韩在线| 欧美一级毛片免费看高清 | 久久久久久亚洲精品影院 | 中文字幕第一页面 | 国产精品爱啪在线线免费观看 | 麻豆影视在线观看 | 亚洲综合激情另类图片专区 | 精品一区二区三区色花堂 | 国产精品天仙tv在线观看 | 国产制服丝袜在线观看 | 国产麻豆传媒视频 | 日本高清不卡一区久久精品 | 久青草免费视频手机在线观看 | 天堂mv亚洲mv在线播放9蜜 | 国产人成精品香港三级在 | 欧美不在线 | 日韩专区在线 | 高清影院|精品秒播3 | 美女任你躁免费视频 | 在线观看视频一区二区 | 亚洲色图男人天堂 | 亚州毛色毛片免费观看 | 国产午夜精品片一区二区三区 | 毛片网站在线观看 | 亚洲一区亚洲二区亚洲三区 | 成人国产在线视频在线观看 | 亚洲中国日本韩国美国毛片 | 久久中文字幕久久久久 | 亚洲免费人成在线视频观看 | 国产精品高清视亚洲乱码 | 网站免费在线观看 | 看黄视频在线观看 | 精品999视频 | 中文字幕一区二区三区免费看 | 中文字幕精品视频在线观看 | 亚洲人成黄网在线观看 | 日韩亚洲一区二区三区 | 国产精品手机视频一区二区 | 免费看黄大全 | 草逼视频免费看 |