【工具类】HexUtil 工具类

该代码段展示了名为HexUtil的工具类,主要用于十六进制字符串与字节数组之间的转换。类中包含方法如hexStringToByte将16进制字符串转换为字节数组,以及bytesToHexString将字节数组转换为16进制字符串。此外,还有辅助方法处理单个字符到字节的转换及字节到16位整数的转换等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HexUtil 源代码 ,代码如下:

/**
 * 十六进制处理工具类
 * 
 */
public class HexUtil {

	/**
	 * 功能描述:将16进制的字符串转换为字节数组,例如有16进制字符串"12345678"<br/>
	 * 转换后的结果为:{18, 52 ,86 ,120 };
	 * 
	 * @param hex
	 *            需要转换的16进制字符串
	 * @return 以字节数组返回转换后的结果
	 */
	public static byte[] hexStringToByte(String hex) {
		int len = (hex.length() / 2);
		byte[] result = new byte[len];
		char[] achar = hex.toUpperCase().toCharArray();
		for (int i = 0; i < len; i++) {
			int pos = i * 2;
			result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
		}
		return result;
	}

	/**
	 * 功能描述:把字节数组转换为十六进制字符串,例如有字节数组<br/>
	 * byte [] data = new byte[]{18, 52 ,86 ,120 };转换之后的结果为:"12 34 56 78"
	 * 
	 * @param bArray
	 *            所要进行转换的数组内容
	 * @return 返回转换后的结果,内容用空格隔开
	 */
	public static final String bytesToHexString(byte[] bArray) {
		StringBuffer sb = new StringBuffer(bArray.length);
		String sTemp;
		int j = 0; // 此处定义的j用于控制每行输出的数据个�?
		for (int i = 0; i < bArray.length; i++) {
			sTemp = Integer.toHexString(0xFF & bArray[i]);
			if (sTemp.length() < 2) {
				sb.append(0);
			}
			sb.append(sTemp.toUpperCase());
			j++;
		}
		return sb.toString();
	}

	/**
	 * 十六进制字符转换成十六进制字节
	 * 
	 * @param c
	 *            十六进制字符
	 * @return 返回十六进制字节
	 */
	private static byte toByte(char c) {
		byte b = (byte) "0123456789ABCDEF".indexOf(c);
		return b;
	}

	/**
	 * 将16位int转换为长度为2的byte数组
	 * 
	 * @param num
	 *            整数值
	 * @return
	 */
	public static byte[] int2bytes(int num) {
		byte[] b = new byte[4];
		int mask = 0xff;
		for (int i = 0; i < 4; i++) {
			b[i] = (byte) (num >>> (24 - i * 8));
		}
		return b;
	}

	/**
	 * 将长度为2的byte数组转换为16位int
	 * 
	 * @param b
	 *            字节数组
	 * @return
	 */
	public static int bytes2int(byte[] b) {
		// byte[] b=new byte[]{1,2,3,4};
		int mask = 0xff;
		int temp = 0;
		int res = 0;
		for (int i = 0; i < 4; i++) {
			res <<= 8;
			temp = b[i] & mask;
			res |= temp;
		}
		return res;
	}
	/**
	 * 将byte数组转换为整数
	 *
	 * @param res
	 * @return
	 */
	public static int bytesToInt(byte[] res) {
		// 一个byte数据左移24位变成0x??000000,再右移8位变成0x00??0000
		int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00) // | 表示安位或
				| ((res[2] << 24) >>> 8) | (res[3] << 24);
		return targets;
	}
	/**
	 * 将长度为2的byte数组转换为16位int
	 * 
	 * @param res
	 *            byte[]
	 * @return int
	 * */
	public static int bytes2short(byte[] b) {
		// byte[] b=new byte[]{1,2,3,4};
		int mask = 0xff;
		int temp = 0;
		int res = 0;
		for (int i = 0; i < 2; i++) {
			res <<= 8;
			temp = b[i] & mask;
			res |= temp;
		}
		return res;
	}

	/**
	 * bcd码转换为字符串
	 * 
	 * @param bcds
	 * @return
	 */
	public static String bcd2str(byte[] bcds) {
		char[] ascii = "0123456789abcdef".toCharArray();
		byte[] temp = new byte[bcds.length * 2];
		for (int i = 0; i < bcds.length; i++) {
			temp[i * 2] = (byte) ((bcds[i] >> 4) & 0x0f);
			temp[i * 2 + 1] = (byte) (bcds[i] & 0x0f);
		}
		StringBuffer res = new StringBuffer();

		for (int i = 0; i < temp.length; i++) {
			res.append(ascii[temp[i]]);
		}
		return res.toString().toUpperCase();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值