Source-Code
public class getUTF8Charset{
/**
* 字符串转UTF-8编码
* @param str 要转换的字符串
* @return 所得的UTF8串
*/
public String stringToUTF8(String str){
return this.byteToUTF8(str.getBytes());
}
/**
* byte转换为16进制
*/
public String byteToUTF8(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(bytes[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
}
Tips
该方法会转换字符串中的英文字符