java-16进制转字符串或者ASCII码

例如:564E3A312D302E302E30  可转换为:VN:1-0.0.0

/**
	 * The conversion of 16 to ASCII
	 * @other > Integer.toHexString(int) -> 10 to 16
	 * @param hex
	 * @return
	 */
	public static String convertHexToString(String hex) {

		StringBuilder sb = new StringBuilder();
		StringBuilder temp = new StringBuilder();

		// 564e3a322d302e312e34 split into two characters 56, 4e, 3a...
		for (int i = 0; i < hex.length() - 1; i += 2) {

			// grab the hex in pairs
			String output = hex.substring(i, (i + 2));
			// convert hex to decimal
			int decimal = Integer.parseInt(output, 16);
			// convert the decimal to character
			sb.append((char) decimal);

			temp.append(decimal);
		}
		// System.out.println(sb.toString());
		return sb.toString();
	}

posted @ 2022-01-27 18:36  zhangdaopin  阅读(896)  评论(0编辑  收藏  举报