-
Notifications
You must be signed in to change notification settings - Fork 0
/
HBaseByteParser.java
37 lines (27 loc) · 1.07 KB
/
HBaseByteParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.io.UnsupportedEncodingException;
import org.apache.commons.lang.StringEscapeUtils;
public class HBaseByteParser {
public static void main(String[] args) throws UnsupportedEncodingException {
// TODO Auto-generated method stub
String s = "\\xe6\\x9b\\xbe\\xe4\\xbf\\x8a";
String ab = StringEscapeUtils.escapeJava(s.replace("\\x", ""));
System.out.println(new String(hexStringToBytes(ab), "utf-8"));
}
private static 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 static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
}