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

如题所述

第1个回答  2011-10-22
楼上答得不对,请相信我的方法,已经确认过:

String s="abc";
String s1=URLEncoder.encode(s, "utf-8");本回答被提问者采纳
第2个回答  2018-04-27

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个回答  2011-10-18
String s="xxx";
String s1=new String(s.getBytes("ISO-8859-1"),"utf-8");
第4个回答  2011-10-18
String s="hellow word";
String s1=new String(s.getBytes("ISO-8859-1"),"utf-8");
第5个回答  2013-02-05
我试过了啦。这个方法 也不对啊。

UTF-8好象有些特别。 GB18030应该容易。
相似回答