java之byte与hexString互转

/**
	 * convert byte[] to HexString
	 * 
	 * @param bArray
	 * @param length
	 * @return
	 */
	public static String bytesToHexString(byte[] bArray, int length)
	{
		StringBuffer sb = new StringBuffer(length);
		String sTemp;
		for (int i = 0; i < length; i++) 
		{
			sTemp = Integer.toHexString(0xFF & bArray[i]);
			if (sTemp.length() < 2)
				sb.append(0);
			sb.append(sTemp.toUpperCase());
		}
		return sb.toString();
	}
 
	/**
	 * convert HexString to byte[]
	 * 
	 * @param hex
	 * @return
	 */
	public static byte[] hexStringToByte(String hex)
	{
		int len = (hex.length() / 2);
		byte[] result = new byte[len];
		char[] achar = hex.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;
 
	}
--------------------- 
作者:shareinfo2018 
来源:CSDN 
原文:https://blog.csdn.net/chenyefei/article/details/80962494 
版权声明:本文为博主原创文章,转载请附上博文链接!

 

你可能感兴趣的:(JAVA开发)