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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
| public class EmojiUtil {
public static String[] ios5emoji ; public static String[] ios4emoji ; public static String[] androidnullemoji ; public static String[] adsbuniemoji;
public static void initios5emoji(String[] i5emj,String[] i4emj,String[] adnullemoji,String[] adsbemoji){ ios5emoji = i5emj; ios4emoji = i4emj; androidnullemoji = adnullemoji; adsbuniemoji = adsbemoji; }
public static String transToIOS4emoji(String src) { return StringUtils.replaceEach(src, ios5emoji, ios4emoji); } public static String transToIOS5emoji(String src) { return StringUtils.replaceEach(src, ios4emoji, ios5emoji); } public static String transToAndroidemojiNull(String src) { return StringUtils.replaceEach(src, ios5emoji, androidnullemoji); }
public static String transToAndroidemojiSB(String src) { return StringUtils.replaceEach(src, ios5emoji, adsbuniemoji); }
public static String transSBToIOS5emoji(String src) { return StringUtils.replaceEach(src, adsbuniemoji, ios5emoji); }
public static String hexstr2String(String hexstr) throws UnsupportedEncodingException{ byte[] b = hexstr2bytes(hexstr); return new String(b, "UTF-8"); }
public static String sbunicode2utfString(String sbhexstr) throws UnsupportedEncodingException{ byte[] b = sbunicode2utfbytes(sbhexstr); return new String(b, "UTF-8"); }
public static byte[] hexstr2bytes(String hexstr){ String[] hexstrs = hexstr.split(" "); byte[] b = new byte[hexstrs.length];
for(int i=0;i<hexstrs.length;i++){ b[i] = hexStringToByte(hexstrs[i].substring(2))[0]; } return b; }
public static byte[] sbunicode2utfbytes(String sbhexstr) throws UnsupportedEncodingException{ int inthex = Integer.parseInt(sbhexstr, 16); char[] schar = {(char)inthex}; byte[] b = (new String(schar)).getBytes("UTF-8"); return b; }
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; }
private static byte toByte(char c) { byte b = (byte) "0123456789ABCDEF".indexOf(c); return b; }
public static String resolveToByteFromEmoji(String str) { Pattern pattern = Pattern .compile("[^(\u2E80-\u9FFF\\w\\s`~!@#\\$%\\^&\\*\\(\\)_+-?()——=\\[\\]{}\\|;。,、《》”:;“!……’:'\"<,>\\.?/\\\\*)]"); Matcher matcher = pattern.matcher(str); StringBuffer sb2 = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(sb2, resolveToByte(matcher.group(0))); } matcher.appendTail(sb2); return sb2.toString(); }
public static String resolveToEmojiFromByte(String str) { Pattern pattern2 = Pattern.compile("<:([[-]\\d*[,]]+):>"); Matcher matcher2 = pattern2.matcher(str); StringBuffer sb3 = new StringBuffer(); while (matcher2.find()) { matcher2.appendReplacement(sb3, resolveToEmoji(matcher2.group(0))); } matcher2.appendTail(sb3); return sb3.toString(); }
private static String resolveToByte(String str) { byte[] b = str.getBytes(); StringBuffer sb = new StringBuffer(); sb.append("<:"); for (int i = 0; i < b.length; i++) { if (i < b.length - 1) { sb.append(Byte.valueOf(b[i]).toString() + ","); } else { sb.append(Byte.valueOf(b[i]).toString()); } } sb.append(":>"); return sb.toString(); }
private static String resolveToEmoji(String str) { str = str.replaceAll("<:", "").replaceAll(":>", ""); String[] s = str.split(","); byte[] b = new byte[s.length]; for (int i = 0; i < s.length; i++) { b[i] = Byte.valueOf(s[i]); } return new String(b); }
public static void main(String[] args) throws UnsupportedEncodingException { byte[] b1 = {-30,-102,-67}; byte[] b2 = {-18,-128,-104};
byte[] b3 = {-16,-97,-113,-128}; byte[] b4 = {-18,-112,-86};
ios5emoji = new String[]{new String(b1,"utf-8"),new String(b3,"utf-8")}; ios4emoji = new String[]{new String(b2,"utf-8"),new String(b4,"utf-8")};
byte[] testbytes = {105,111,115,-30,-102,-67,32,36,-18,-128,-104,32,36,-16,-97,-113,-128,32,36,-18,-112,-86}; String tmpstr = new String(testbytes,"utf-8"); System.out.println(tmpstr);
String ios4str = transToIOS5emoji(tmpstr); byte[] tmp = ios4str.getBytes(); for(byte b:tmp){ System.out.print(b); System.out.print(" "); } }
}
|