将16进制的字符串 序列,转换 为汉子字符串


方法一:(byte) (0xff & Integer.parseInt(strings[i], 16));// 获得当前16进制字符串对应的 byte数值 

添加到byte数组,然后 以指定编码 转换:new String(hanzi, "utf-8")。


String string = "53+ff+55+23+04+00+21+" 
								+ "e7+88+b1+e5+a4+"
								+ "9a+e6+b7+b1+e7+" 
								+ "97+9b+e5+a4+9a+" 
								+ "e4+b9+85+2d+e4+" 
								+ "b8+9c+e6+9d+a5+"	
								+ "e4+b8+9c+e5+be+"
								+ "80+00+" 
								+ "e1";

		String[] strings = string.split("\\+");
		int length = Integer.parseInt(strings[3], 16);
		StringBuffer buffer = new StringBuffer();
		byte hanzi[] = new byte[length - 3];
		for (int i = 7; i < length - 3 + 7; i++) {
			hanzi[i - 7] = (byte) (0xff & Integer.parseInt(strings[i], 16));// 获得当前16进制字符串对应的
																			// byte数值
		}
		try {
			// 以utf-8 编码 byte数组
			System.out.println(new String(hanzi, "utf-8"));

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

方法二:

更具通用性

private static String hexString = "0123456789ABCDEF";

	public static String decode(String bytes) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length() / 2);
		// 将每2位16进制整数组装成一个字节
		for (int i = 0; i < bytes.length(); i += 2)
			//获得当前16进制字符串(如 e7) 的byte 值
			baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString.indexOf(bytes.charAt(i + 1))));
		return new String(baos.toByteArray());
	}


你可能感兴趣的:(将16进制的字符串 序列,转换 为汉子字符串)