选中文件右键-->Properties-->Resource-->Text file encoding-->选中Other,然后选择自己想要的编码格式就可以了。
如果是修改MyEclipse的编码方式,可以按以下步骤:
菜单栏的Window-->Perferences-->General-->Work Space-->Text file encoding-->选中Other,然后选择自己想要的编码格式就可以了。
中文转换成Unicode编码和Unicode编码转换成中文
import javautilProperties;public class Test {
public static void main(String[] args) {
String s = "简介";
String tt = gbEncoding(s); // String tt1 = "你好";
Systemoutprintln(decodeUnicode("\\u7b80\\u4ecb")); // Systemoutprintln(decodeUnicode(tt1));
Systemoutprintln(HTMLDecoderdecode("中国"));
String s1 = "\u7b80\u4ecb";
Systemoutprintln(sindexOf("\\"));
}
public static String gbEncoding(final String gbString) {
char[] utfBytes = gbStringtoCharArray();
String unicodeBytes = "";
for (int byteIndex = 0; byteIndex < utfByteslength; byteIndex++) {
String hexB = IntegertoHexString(utfBytes[byteIndex]);
if (hexBlength() <= 2) {
hexB = "00" + hexB;
}
unicodeBytes = unicodeBytes + "\\u" + hexB;
}
Systemoutprintln("unicodeBytes is: " + unicodeBytes);
return unicodeBytes; }
public static String decodeUnicode(final String dataStr) {
int start = 0;
int end = 0;
final StringBuffer buffer = new StringBuffer();
while (start > -1) {
end = dataStrindexOf("\\u", start + 2);
String charStr = "";
if (end == -1) {
charStr = dataStrsubstring(start + 2, dataStrlength());
} else {
charStr = dataStrsubstring(start + 2, end);
}
char letter = (char) IntegerparseInt(charStr, 16); // 16进制parse整形字符串。
bufferappend(new Character(letter)toString());
start = end;
}
return buffertoString();
} }
}
}
}
方法一:
思路:先转为Unicode,然后转为GBKString utf8 = new String(tgetBytes( "UTF-8"));
Systemoutprintln(utf8);
String unicode = new String(utf8getBytes(),"UTF-8");
Systemoutprintln(unicode);
String gbk = new String(unicodegetBytes("GBK"));
Systemoutprintln(gbk);
方法二:
public static void main(String[] args) {String str="字符串编码转换";
try {
byte[] temp=strgetBytes("utf-8");//这里写原编码方式
byte[] newtemp=new String(temp,"utf-8")getBytes("gbk");//这里写转换后的编码方式
String newStr=new String(newtemp,"gbk");//这里写转换后的编码方式
Systemoutprintln(newStr);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)