实现字符串的编码转换,用以解决字符串乱码问题
生活随笔
收集整理的這篇文章主要介紹了
实现字符串的编码转换,用以解决字符串乱码问题
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
引起亂碼的情況很多~實(shí)質(zhì)上 主要是字符串本身的編碼格式 與程序所需要的編碼格式不一致導(dǎo)致的。要解決亂碼其實(shí)很簡單,
分2步 :
1:獲取到字符串 本身的編碼
2:改變字符串編碼 (本身編碼 -> 新編碼)
話不多說,直接貼代碼
package cn.sccl.framework.util;import org.apache.commons.lang3.StringUtils;import java.io.UnsupportedEncodingException; /*** 字符編碼工具類** @author _minus* @create 2017-11-06 18:38*/ public class CharsetUtils {private enum Charset {/** 7位ASCII字符,也叫作ISO646-US、Unicode字符集的基本拉丁塊 */US_ASCII("US-ASCII","位ASCII字符,也叫作ISO646-US、Unicode字符集的基本拉丁塊 "),ISO_8859_1("ISO-8859-1","ISO 拉丁字母表 No.1,也叫作 ISO-LATIN-1"),UTF_8("UTF-8","8 位 UCS 轉(zhuǎn)換格式"),UTF_16BE("UTF-16BE","16 位 UCS 轉(zhuǎn)換格式,Big Endian(最低地址存放高位字節(jié))字節(jié)順序"),UTF_16LE("UTF_16LE","16 位 UCS 轉(zhuǎn)換格式,Big Endian(最低地址存放高位字節(jié))字節(jié)順序"),UTF_16("UTF_16","16 位 UCS 轉(zhuǎn)換格式,字節(jié)順序由可選的字節(jié)順序標(biāo)記來標(biāo)識"),GBK("GBK","中文超大字符集");private String encode;private String desc;public String getEncode() {return encode;}public void setEncode(String encode) {this.encode = encode;}public String getDesc() {return desc;}public void setDesc(String desc) {this.desc = desc;}private Charset(String encode, String desc){this.encode =encode;this.desc = desc ;}}/*** 獲取傳入字符串的編碼格式* @param str* @return*/public static String getEncode(String str) throws UnsupportedEncodingException {if (!StringUtils.isEmpty(str)){for (Charset charset : Charset.values()) {if (str.equals(new String(str.getBytes(charset.getEncode()),charset.getEncode()))){return charset.getEncode();}}}throw new UnsupportedEncodingException("編碼庫中不存在");}/*** 字符串編碼轉(zhuǎn)換的實(shí)現(xiàn)方法* @param str 待轉(zhuǎn)換編碼的字符串* @param newCharset 目標(biāo)編碼* @return* @throws UnsupportedEncodingException*/public static String changeCharset(String str, String newCharset)throws UnsupportedEncodingException {if (str != null) {//獲取到原字符編碼String charsetName = getEncode(str);//用默認(rèn)字符編碼解碼字符串。byte[] bs = str.getBytes(charsetName);//用新的字符編碼生成字符串return new String(bs, newCharset);}return null;}/*** 將字符編碼轉(zhuǎn)換成US-ASCII碼*/public static String toASCII(String str) throws UnsupportedEncodingException {return changeCharset(str, Charset.US_ASCII.getEncode());}/*** 將字符編碼轉(zhuǎn)換成ISO-8859-1碼*/public static String toISO_8859_1(String str) throws UnsupportedEncodingException {return changeCharset(str, Charset.ISO_8859_1.getEncode());}/*** 將字符編碼轉(zhuǎn)換成UTF-8碼*/public static String toUTF_8(String str) throws UnsupportedEncodingException {return changeCharset(str, Charset.UTF_8.getEncode());}/*** 將字符編碼轉(zhuǎn)換成UTF-16BE碼*/public static String toUTF_16BE(String str) throws UnsupportedEncodingException {return changeCharset(str, Charset.UTF_16BE.getEncode());}/*** 將字符編碼轉(zhuǎn)換成UTF-16LE碼*/public static String toUTF_16LE(String str) throws UnsupportedEncodingException {return changeCharset(str, Charset.UTF_16LE.getEncode());}/*** 將字符編碼轉(zhuǎn)換成UTF-16碼*/public static String toUTF_16(String str) throws UnsupportedEncodingException {return changeCharset(str,Charset.UTF_16.getEncode());}/*** 將字符編碼轉(zhuǎn)換成GBK碼*/public static String toGBK(String str) throws UnsupportedEncodingException {return changeCharset(str, Charset.GBK.getEncode());}}?
轉(zhuǎn)載于:https://www.cnblogs.com/hsc13-lxy14/p/7794682.html
總結(jié)
以上是生活随笔為你收集整理的实现字符串的编码转换,用以解决字符串乱码问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: noip模拟赛 写代码
- 下一篇: python的字符串内建函数