Java 十六进制字符与byte数组转换

最近项目中需要把字节数组转换为十六进制数的形式,研究了好久终于实现了,分享一下


字节数组与十六进制字符串之间的相互转换

/**
	 * Convert byte[] to hex
	 * string.这里我们可以将byte转换成int,然后利用Integer.toHexString(int)来转换成16进制字符串。
	 * @param src
	 * @return
	 */
	public String bytesToHexString(byte[] src) {
		StringBuilder stringBuilder = new StringBuilder("");
		if (src == null || src.length <= 0) {
			return null;
		}
		for (int i = 0; i < src.length; i++) {
			int v = src[i] & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}
		return stringBuilder.toString();
	}

	/**
	 * Convert hex string to byte[]
	 * 
	 * @param hexString
	 * @return
	 */
	public byte[] hexStringToBytes(String hexString) {
		if (hexString == null || hexString.equals("")) {
			return null;
		}
//		hexString = hexString.toUpperCase();	//如果是大写形式
		int length = hexString.length() / 2;
		char[] hexChars = hexString.toCharArray();
		byte[] d = new byte[length];
		for (int i = 0; i < length; i++) {
			int pos = i * 2;
			d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
		}
		return d;
	}

	private byte charToByte(char c) {
		return (byte) "0123456789abcdef".indexOf(c);
//		return (byte) "0123456789ABCDEF".indexOf(c);
	}



字符串与其相应的UTF-8编码之间的转换

/**
	 * convert UTF-8 code string to String,能处理汉字
	 * 
	 * @param s
	 * @return
	 */
	public static String convertUTF8ToString(String s) {
		if (s == null || s.equals("")) {
			return null;
		}

		try {

			s = s.toUpperCase();

			int total = s.length() / 2;
			int pos = 0;

			byte[] buffer = new byte[total];
			for (int i = 0; i < total; i++) {

				int start = i * 2;

				buffer[i] = (byte) Integer.parseInt(
						s.substring(start, start + 2), 16);
				pos++;
			}

			return new String(buffer, 0, pos, "UTF-8");

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return s;
	}

	/**
	 * convert String to UTF code string,能处理汉字
	 * 
	 * @param s
	 *            原串
	 * @return
	 */
	public static String convertStringToUTF8(String s) {
		if (s == null || s.equals("")) {
			return null;
		}
		StringBuffer sb = new StringBuffer();
		try {
			char c;
			for (int i = 0; i < s.length(); i++) {
				c = s.charAt(i);
				byte[] b = Character.toString(c).getBytes("utf-8");
				if (c >= 0 && c <= 255) {	//123...9 a-z A-Z
					
					for (int j = 0; j < b.length; j++) {
						int k = b[j];
						if (k < 0)
							k += 256;
						sb.append("0000"+Integer.toHexString(k).toUpperCase());	//考虑到ASICII码字符的情况
//						sb.append(Integer.toHexString(k).toUpperCase());
					}
					
				} else {
					
					for (int j = 0; j < b.length; j++) {
						int k = b[j];
						if (k < 0)
							k += 256;
						sb.append(Integer.toHexString(k).toUpperCase());
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();

		}
		return sb.toString();
	}


将字节数组转换为Long

/*
	 * byte[]->long
	 */
	public final static long getLong(byte[] buf, boolean asc) {
		if (buf == null) {
			throw new IllegalArgumentException("byte array is null!");
		}
		if (buf.length > 8) {
			throw new IllegalArgumentException("byte array size > 8 !");
		}
		long r = 0;
		if (asc)
			for (int i = buf.length - 1; i >= 0; i--) {
				r <<= 8;
				r |= (buf[i] & 0x000000ff);
			}
		else
			for (int i = 0; i < buf.length; i++) {
				r <<= 8;
				r |= (buf[i] & 0x000000ff);
			}
		return r;
	}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值