解决了困扰了很久的问题。才疏学浅,留个记忆。
从jni收到char[]类型数据,中文乱码
char[] obj.data
String str = String.copyValueOf(obj.data) ;
String[] _Info = str.split(",");
for(int i=0,i<_Info .length , i++)
alias=alias+Integer.toHexString((int)_Info [1].charAt(i));
接下去用下面的方法就行
原文:http://blog.csdn.net/top_code/article/details/8450423
最近项目中需要把中文转换为UTF-8编码,并且还能将转换后的UTF-8编码转换为原来的中文,比如 上海 转换为UTF-8编码为 E4B88AE6B5B7,
Google了不少时间,然后参考 JDK源码 实现了上述功能
代码如下:
-
-
-
-
-
-
-
-
-
-
-
-
- public static String convertUTF8ToString(String s) {
- if (s == null || s.equals("")) {
- return null;
- }
-
- try {
- s = s.toUpperCase();
-
- int total = s.length() / 2;
- int pos = 0;
-
- byte[] buffer = new byte[total];
- for (int i = 0; i < total; i++) {
-
- int start = i * 2;
-
- buffer[i] = (byte) Integer.parseInt(
- s.substring(start, start + 2), 16);
- pos++;
- }
-
- return new String(buffer, 0, pos, "UTF-8");
-
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- return s;
- }
-
-
-
-
-
-
-
- public static String convertStringToUTF8(String s) {
- if (s == null || s.equals("")) {
- return null;
- }
- StringBuffer sb = new StringBuffer();
- try {
- char c;
- for (int i = 0; i < s.length(); i++) {
- c = s.charAt(i);
- if (c >= 0 && c <= 255) {
- sb.append(c);
- } else {
- byte[] b;
-
- b = Character.toString(c).getBytes("utf-8");
-
- for (int j = 0; j < b.length; j++) {
- int k = b[j];
- if (k < 0)
- k += 256;
- sb.append(Integer.toHexString(k).toUpperCase());
-
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
-
- }
- return sb.toString();
- }