比如一个十六进制的字符串 b7c5b4f3 ,我如何把他转换成中文
跪求高手解释
------解决方案--------------------
- Java code
try { String s = "b7c5b4f3"; //思路就是把String转成对应的byte[]数组 byte[] b = new byte[s.length()/2]; for (int i=0, j=0; i<s.length(); i+=2) { b[j++] = (byte)Integer.parseInt(s.substring(i,i+2), 16); } String data = new String(b, "UTF-16BE"); //自己调整编码试试看,如UTF-16LE什么的? System.out.println(data);} catch (Exception e) { e.printStackTrace();}
------解决方案--------------------
- Java code
package com;import java.io.UnsupportedEncodingException;import java.util.Scanner;public class Decoder {public static void main(String[]args){ System.out.println("请输入转换的16进制代码:"); Scanner scan=new Scanner(System.in); String getb=scan.nextLine(); String result=decodeString(getb, "GBK"); System.out.println(result);} private static String decodeString(String string, String encoding) {try {byte[]data=string2Bytes(string);return new String(data, encoding);} catch (UnsupportedEncodingException ex) {ex.printStackTrace();}return encoding;} private static byte[] string2Bytes(String string) {int blen=string.length()/2;byte[]data=new byte[blen];for(int i=0;i<blen;i++){String Str=string.substring(2*i,2*(i+1));data[i]=(byte)Integer.parseInt(Str,16);}return data;}}
------解决方案--------------------
必须先知道 你这个字符串是什么编码 utf-8