java 怎么把一个字符串进行utf8编码

2025-02-27 20:09:28
推荐回答(5个)
回答1:

楼上答得不对,请相信我的方法,已经确认过:

String s="abc";
String s1=URLEncoder.encode(s, "utf-8");

回答2:

String text = “字符串”;

byte[] b_utf8 = text.getBytes("UTF-8"); //utf-8

byte[] b_iso88591 = text.getBytes("ISO8859-1"); //iso8859-1

byte[] b_gbk = text.getBytes("GBK"); //gbk

string unicode = getUnicode(text);//unicode

public static String getUnicode(String source) {

String result = "";

for (int i = 0; i < source.length(); i++) {

result += "\\u"+Integer.toHexString((int) source.charAt(i));

}

return result;

}

回答3:

String s="xxx";
String s1=new String(s.getBytes("ISO-8859-1"),"utf-8");

回答4:

String s="hellow word";
String s1=new String(s.getBytes("ISO-8859-1"),"utf-8");

回答5:

我试过了啦。这个方法 也不对啊。

UTF-8好象有些特别。 GB18030应该容易。