字元編碼轉換
import java.io.UnsupportedEncodingException;
public class ChangeCharset {
public static final String US_ASCII = "US-ASCII";
public static final String ISO_8859_1 = "ISO-8859-1";
public static final String UTF_8 = "UTF-8";
public static final String UTF_16BE = "UTF-16BE";
public static final String UTF_16LE = "UTF-16LE";
public static final String UTF_16 = "UTF-16";
public static final String GBK = "GBK";
public String toASCII(String str) throws UnsupportedEncodingException {
return this.changeCharset(str, US_ASCII);
}
public String toISO_8859_1(String str) throws UnsupportedEncodingException {
return this.changeCharset(str, ISO_8859_1);
}
public String toUTF_8(String str) throws UnsupportedEncodingException {
return this.changeCharset(str, UTF_8);
}
public String toUTF_16BE(String str) throws UnsupportedEncodingException {
return this.changeCharset(str, UTF_16BE);
}
public String toUTF_16LE(String str) throws UnsupportedEncodingException {
return this.changeCharset(str, UTF_16LE);
}
public String toUTF_16(String str) throws UnsupportedEncodingException {
return this.changeCharset(str, UTF_16);
}
public String toGBK(String str) throws UnsupportedEncodingException {
return this.changeCharset(str, GBK);
}
public String changeCharset(String str, String newCharset)
throws UnsupportedEncodingException {
if (str != null) {
// 用預設字元編碼解碼字串。
byte[] bs = str.getBytes();
// 用新的字元編碼生成字串
return new String(bs, newCharset);
}
return null;
}
public String changeCharset(String str, String oldCharset, String newCharset)
throws UnsupportedEncodingException {
if (str != null) {
// 用舊的字元編碼解碼字串。解碼可能會出現異常。
byte[] bs = str.getBytes(oldCharset);
// 用新的字元編碼生成字串
return new String(bs, newCharset);
}
return null;
}
}
相關文章
- java字元編碼轉換Java字元
- javascript字元編碼轉換方法JavaScript字元
- iconv字元編碼轉換字元
- php 的字元編碼轉換工具 (轉)PHP字元
- Python 字元編碼轉換祕訣Python字元
- C# 基礎知識:字元編碼、編碼轉換C#字元
- Linux 字元編碼 檢視與轉換Linux字元
- javascript 字元轉換為ascii碼,ascii碼轉換為字元JavaScript字元ASCII
- PHP中文字元gbk編碼與UTF-8編碼的轉換PHP字元
- 編碼轉換
- URL編碼轉換
- ascii碼與字元的相互轉換ASCII字元
- 【字元編碼】徹底理解字元編碼字元
- 【字元編碼】字元編碼 && Base64編碼演算法字元演算法
- 字元編碼字元
- 位元組編碼轉換
- 文字編碼轉換工具iconv 附批量轉換檔案編碼命令
- 字串-字元編碼字串字元
- 聊聊字元編碼字元
- vim字元編碼字元
- 字元編碼 and cpp字元
- 字元型別轉換字元型別
- Linux批量編碼轉換Linux
- java編碼之間轉換Java
- scheme跳轉特殊字元編碼問題Scheme字元
- 檢測檔案編碼,轉換檔案編碼
- ptyon 特殊處理 url 編碼與解碼,字元編碼轉化 unicode字元Unicode
- 【字元編碼】Java編碼格式探祕字元Java
- 圖解字元編碼圖解字元
- 理解字元編碼字元
- php_字元編碼PHP字元
- 關於字元編碼字元
- 字元編碼淺談字元
- 字元編碼再次剖析字元
- URL地址特殊字元轉換字元
- 數值轉換為字元字元
- 【字元編碼】Java字元編碼詳細解答及問題探討字元Java
- 字符集編碼(二):字元編碼模型字元模型