import org.apache.hadoop.hbase.shaded.org.apache.commons.codec.DecoderException; import org.apache.hadoop.hbase.shaded.org.apache.commons.codec.binary.Hex; import java.io.UnsupportedEncodingException; /** * @Author weijun.nie * @Date 2020/2/27 15:50 * @Version 1.0 */ public class HbaseBytesUtil { public static void main(String[] args) throws UnsupportedEncodingException { String hbase1 = "\\xE4\\xB8\\x81\\xE4\\xBD\\xB3\\xE5\\xA6\\xAE"; String hbase2 = "\\xE7\\x89\\x9B\\xE5\\x8F\\x89"; System.out.println(getHanzByHexString(hbase1)); System.out.println(getHanzByHexString(hbase2)); String s1 = "牛叉"; System.out.println(getHexStringByHaz(s1)); } /** * "\xE7\x89\x9B\xE5\x8F\x89" --> "E7899BE58F89" --> "牛叉" * <p> * 把hbase中的漢字 16進制字符串 拿出, 轉換爲漢字 * hexString相似: \xE4\xB8\x81\xE4\xBD\xB3\xE5\xA6\xAE * * @param hexString * @return 漢字 */ public static String getHanzByHexString(String hexString) { // 1. 去掉"\x" \xE4\xB8\x81\xE4\xBD\xB3\xE5\xA6\xAE hexString = hexString.replaceAll("\\\\x", ""); System.out.println(hexString); char[] chars = hexString.toCharArray(); String ouputStr = null; try { ouputStr = new String(Hex.decodeHex(chars), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (DecoderException e) { e.printStackTrace(); } return ouputStr; } /** * "牛叉"-->"E7899BE58F89" * * @param hanz * @return */ public static String getHexStringByHaz(String hanz) { byte[] utf8Bytes = null; // 1. 漢字轉 字節數組--> utf-8 try { utf8Bytes = hanz.getBytes("UTF-8"); } catch (Exception e) { e.printStackTrace(); } // 2. 每一個字節轉爲十六進制; 加個 \x StringBuffer sb = new StringBuffer(); for (int i = 0; i < utf8Bytes.length; i++) { sb.append("\\x").append(Integer.toHexString(utf8Bytes[i] & 0xFF)); } return sb.toString().toUpperCase(); } }
---[2020.02.28 16:14:47]---java