java编码转化方案-备用

  1 import java.io.UnsupportedEncodingException;

  2 

  3 /**

  4  * 转换字符串的编码

  5  */

  6 public class changeCharSet {

  7  /** 7位ASCII字符,也叫作ISO646-US、Unicode字符集的基本拉丁块 */

  8  public static final String US_ASCII = "US-ASCII";

  9 

 10  /** ISO 拉丁字母表 No.1,也叫作 ISO-LATIN-1 */

 11  public static final String ISO_8859_1 = "ISO-8859-1";

 12 

 13  /** 8 位 UCS 转换格式 */

 14  public static final String UTF_8 = "UTF-8";

 15 

 16  /** 16 位 UCS 转换格式,Big Endian(最低地址存放高位字节)字节顺序 */

 17  public static final String UTF_16BE = "UTF-16BE";

 18 

 19  /** 16 位 UCS 转换格式,Little-endian(最高地址存放低位字节)字节顺序 */

 20  public static final String UTF_16LE = "UTF-16LE";

 21 

 22  /** 16 位 UCS 转换格式,字节顺序由可选的字节顺序标记来标识 */

 23  public static final String UTF_16 = "UTF-16";

 24 

 25  /** 中文超大字符集 */

 26  public static final String GBK = "GBK";

 27 

 28  /**

 29   * 将字符编码转换成US-ASCII码

 30   */

 31  public String toASCII(String str) throws UnsupportedEncodingException{

 32   return this.changeCharSet(str, US_ASCII);

 33  }

 34  /**

 35   * 将字符编码转换成ISO-8859-1码

 36   */

 37  public String toISO_8859_1(String str) throws UnsupportedEncodingException{

 38   return this.changeCharSet(str, ISO_8859_1);

 39  }

 40  /**

 41   * 将字符编码转换成UTF-8码

 42   */

 43  public String toUTF_8(String str) throws UnsupportedEncodingException{

 44   return this.changeCharSet(str, UTF_8);

 45  }

 46  /**

 47   * 将字符编码转换成UTF-16BE码

 48   */

 49  public String toUTF_16BE(String str) throws UnsupportedEncodingException{

 50   return this.changeCharSet(str, UTF_16BE);

 51  }

 52  /**

 53   * 将字符编码转换成UTF-16LE码

 54   */

 55  public String toUTF_16LE(String str) throws UnsupportedEncodingException{

 56   return this.changeCharSet(str, UTF_16LE);

 57  }

 58  /**

 59   * 将字符编码转换成UTF-16码

 60   */

 61  public String toUTF_16(String str) throws UnsupportedEncodingException{

 62   return this.changeCharSet(str, UTF_16);

 63  }

 64  /**

 65   * 将字符编码转换成GBK码

 66   */

 67  public String toGBK(String str) throws UnsupportedEncodingException{

 68   return this.changeCharSet(str, GBK);

 69  }

 70  

 71  /**

 72   * 字符串编码转换的实现方法

 73   * @param str  待转换编码的字符串

 74   * @param newCharset 目标编码

 75   * @return

 76   * @throws UnsupportedEncodingException

 77   */

 78  public String changeCharSet(String str, String newCharset)

 79    throws UnsupportedEncodingException {

 80   if (str != null) {

 81    //用默认字符编码解码字符串。

 82    byte[] bs = str.getBytes();

 83    //用新的字符编码生成字符串

 84    return new String(bs, newCharset);

 85   }

 86   return null;

 87  }

 88  /**

 89   * 字符串编码转换的实现方法

 90   * @param str  待转换编码的字符串

 91   * @param oldCharset 原编码

 92   * @param newCharset 目标编码

 93   * @return

 94   * @throws UnsupportedEncodingException

 95   */

 96  public String changeCharSet(String str, String oldCharset, String newCharset)

 97    throws UnsupportedEncodingException {

 98   if (str != null) {

 99    //用旧的字符编码解码字符串。解码可能会出现异常。

100    byte[] bs = str.getBytes(oldCharset);

101    //用新的字符编码生成字符串

102    return new String(bs, newCharset);

103   }

104   return null;

105  }

106 

107  public static void main(String[] args) throws UnsupportedEncodingException {

108   changeCharSet test = new changeCharSet();

109   String str = "This is a 中文的 String!";

110   System.out.println("str: " + str);

111   String gbk = test.toGBK(str);

112   System.out.println("转换成GBK码: " + gbk);

113   System.out.println();

114   String ascii = test.toASCII(str);

115   System.out.println("转换成US-ASCII码: " + ascii);

116   gbk = test.changeCharSet(ascii,changeCharSet.US_ASCII, changeCharSet.GBK);

117   System.out.println("再把ASCII码的字符串转换成GBK码: " + gbk);

118   System.out.println();

119   String iso88591 = test.toISO_8859_1(str);

120   System.out.println("转换成ISO-8859-1码: " + iso88591);

121   gbk = test.changeCharSet(iso88591,changeCharSet.ISO_8859_1, changeCharSet.GBK);

122   System.out.println("再把ISO-8859-1码的字符串转换成GBK码: " + gbk);

123   System.out.println();

124   String utf8 = test.toUTF_8(str);

125   System.out.println("转换成UTF-8码: " + utf8);

126   gbk = test.changeCharSet(utf8,changeCharSet.UTF_8, changeCharSet.GBK);

127   System.out.println("再把UTF-8码的字符串转换成GBK码: " + gbk);

128   System.out.println();

129   String utf16be = test.toUTF_16BE(str);

130   System.out.println("转换成UTF-16BE码:" + utf16be);

131   gbk = test.changeCharSet(utf16be,changeCharSet.UTF_16BE, changeCharSet.GBK);

132   System.out.println("再把UTF-16BE码的字符串转换成GBK码: " + gbk);

133   System.out.println();

134   String utf16le = test.toUTF_16LE(str);

135   System.out.println("转换成UTF-16LE码:" + utf16le);

136   gbk = test.changeCharSet(utf16le,changeCharSet.UTF_16LE, changeCharSet.GBK);

137   System.out.println("再把UTF-16LE码的字符串转换成GBK码: " + gbk);

138   System.out.println();

139   String utf16 = test.toUTF_16(str);

140   System.out.println("转换成UTF-16码:" + utf16);

141   gbk = test.changeCharSet(utf16,changeCharSet.UTF_16LE, changeCharSet.GBK);

142   System.out.println("再把UTF-16码的字符串转换成GBK码: " + gbk);

143   String s = new String("中文".getBytes("UTF-8"),"UTF-8");

144   System.out.println(s);

145  }

146 }

 

你可能感兴趣的:(java)