javabyte数组与16进制间相互转换的⽰例1.准备⼯作
import java.util.Arrays;
/**
* Byte[]与hex的相互转换
* @explain
* @author Marydon
* @creationTime 2018年6⽉11⽇下午2:29:11
* @version 1.0
* @since
* @email marydon20170307@163
*/
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类型数组转化成16进制字符串
⽅法⼀
/**
* ⽅法⼀:将byte类型数组转化成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]);
}
String();
}
⽅法⼆
/
**
* ⽅法⼆: byte[] to hex string
* @explain 使⽤数组
* @param bytes
* @return
*/
public static String toHexString2(byte[] bytes) {
// ⼀个byte为8位,可⽤两个⼗六进制位表⽰
char[] buf = new char[bytes.length * 2];
int a = 0;
int index = 0;
/
/ 使⽤除与取余进⾏转换
for (byte b : bytes) {
if (b < 0)
a = 256 + b;
else
a = b;
// 偶数位⽤商表⽰
buf[index++] = HEX_CHAR[a / 16];
// 奇数位⽤余数表⽰
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;
// 利⽤位运算进⾏转换,可以看作⽅法⼆的变型
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⽅法进⾏转换
for (byte b : bytes) {
sb.append(String.format("%02x", new Integer(b & 0xff)));
}
String();
}
⽅法五
/**
* 将byte数组转换成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 = HexString(v);
if (hv.length() < 2) {
sb.append(0);
}
sb.append(hv);
}
String();
}
3.16进制字符串转换为byte[]
⽅法⼀
/**
* 将16进制字符串转换为byte[]
* @explain 16进制字符串不区分⼤⼩写,返回的数组相同
* @param hexString
* 16进制字符串
* @return byte[]
*/
public static byte[] fromHexString(String hexString) {
if (null == hexString || "".im())) {
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进制转换为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 = UpperCase();
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\":\"wwwblogs/Marydon20170307\"}";
byte[] bytes = Bytes("utf-8");
System.out.println("字节数组为:" + String(bytes));
System.out.println("byte数组转16进制之⽅法⼀:" + toHexString(bytes));
System.out.println("byte数组转16进制之⽅法⼆:" + HexString2(bytes));
System.out.println("byte数组转16进制之⽅法三:" + HexString3(bytes));
System.out.println("byte数组转16进制之⽅法四:" + HexString4(bytes));
System.out.println("==================================");
String str = "7b226e616d65223a224d617279646f6e222c2277656273697465223a22687474703a2f2f7777772e636e626c6f67732e636f6d2f4d617279646f6e3230313730333037227d"; System.out.println("转换后的字节数组:" + String(fromHexString(str)));
System.out.println(new String(fromHexString(str), "utf-8"));
}
补充
1B=8b,也就是1byte=8bit;
1KB=1024B;
1MB=1024KB;
java中字符串转数组 1GB=1024MB;
1TB=1024GB
bit是计算机最⼩的存储单元,只能存储0和1,是Binary digit(⼆进制数位)的缩写,意为“位”或“⽐特”,也就是⼆进制。
以上就是java byte数组与16进制间相互转换的⽰例的详细内容,更多关于java byte数组与16进制间的资料请关注其它相关⽂章!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论