java 编码格式_java转换字符串编码格式的方法

java转换字符串编码格式 (解码错误,重新解码)

字符集概念:规定了某个文字对应的二进制数字存放方式(编码)和某串二进制数值代表了哪个文字(解码)的转换关系。

我们在计算机屏幕上看到的是实体化的文字,而在计算机存储介质中存放的实际是二进制的比特流。

乱码场景(纯属瞎掰):

1) 前台输入utf-8编码的一串汉字(string1)。 (页面编码为utf-8, 在内存中会将这串汉字以utf-8编码为对应的二进制流存储)

2) 这串汉字(string1)的二进制流在经过http协议传输到后台时,这段比特流会被以iso-8859-1编码强行解码为字符串(string2)。

(2.1 http默认编码格式为iso-8859-1)

(2.2 这个默认编码在什么时候起作用呢? 应该是在到达tomcat之后, 到达servlet之前, tomcat对request请求强行使用iso-8859-1进行了解码)

(2.3 有什么办法阻止tomcat对request请求强行iso-8859-1解码呢?

apache-tomcat\conf\server.xml中添加URIEncoding="UTF-8"配置即可,还是来个图吧)

64cc39450769f9e8e669a9c778d4fbca.png

3) 在后台(servlet)接收字符串(string2)时毫无疑问的乱码了。

) 这时需要将接收到的字符串(string2)根据iso-8859-1编码重新转换为byte流。再将byte流根据utf-8编码重新解码为字符串(sting3)。

5) 这时的字符串(string3)和前台的字符串(string1)是对应同一个二进制流,并且使用的是同一种编码。也就不会乱码了。

乱码的另一种解决办法:

request.setCharacterEncoding("UTF-8"),这句话熟悉么,这句话的意思是:用"utf-8"编码对客户端的请求进行重新解码。

在步骤2之后(或步骤3中)执行,那么接收到的参数也不会乱码啦。

一个小例子:

import java.io.UnsupportedEncodingException;

public class ConvertEncodingFormat {

/**

* 将一段错误解码的字符串重新解码

*/

public static String convertEncodingFormat(String str, String formatFrom, String FormatTo) {

String result = null;

if (!(str == null || str.length() == 0)) {

try {

result = new String(str.getBytes(formatFrom), FormatTo);

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

}

return result;

}

/**

* test

*/

public static void main(String[] args) {

// utf-8编码

String str = "你好,少年!";

// UTF-8编码的byte流强行用iso-8859-1解码,毫无疑问的乱码了

String str1 = convertEncodingFormat(str, "UTF-8", "iso-8859-1");

System.out.println(str1);

// 将str1再转化为byte流,重新用UTF-8解码,乱码问题解决

String str2 = convertEncodingFormat(str1, "iso-8859-1", "UTF-8");

System.out.println(str2);

}

}

java字符串的各种编码转换

import java.io.UnsupportedEncodingException;

/**

* 转换字符串的编码

*/

public class ChangeCharset {

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

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

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

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

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

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

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

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

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

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

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

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

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

public static final String GBK = "GBK";

/**

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

*/

public String toASCII(String str) throws UnsupportedEncodingException{

return this.changeCharset(str, US_ASCII);

}

/**

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

*/

public String toISO_8859_1(String str) throws UnsupportedEncodingException{

return this.changeCharset(str, ISO_8859_1);

}

/**

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

*/

public String toUTF_8(String str) throws UnsupportedEncodingException{

return this.changeCharset(str, UTF_8);

}

/**

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

*/

public String toUTF_16BE(String str) throws UnsupportedEncodingException{

return this.changeCharset(str, UTF_16BE);

}

/**

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

*/

public String toUTF_16LE(String str) throws UnsupportedEncodingException{

return this.changeCharset(str, UTF_16LE);

}

/**

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

*/

public String toUTF_16(String str) throws UnsupportedEncodingException{

return this.changeCharset(str, UTF_16);

}

/**

* 将字符编码转换成GBK码

*/

public String toGBK(String str) throws UnsupportedEncodingException{

return this.changeCharset(str, GBK);

}

/**

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

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

* @param newCharset 目标编码

* @return

* @throws UnsupportedEncodingException

*/

public String changeCharset(String str, String newCharset)

throws UnsupportedEncodingException {

if (str != null) {

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

byte[] bs = str.getBytes();

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

return new String(bs, newCharset);

}

return null;

}

/**

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

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

* @param oldCharset 原编码

* @param newCharset 目标编码

* @return

* @throws UnsupportedEncodingException

*/

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;

}

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

ChangeCharset test = new ChangeCharset();

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

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

String gbk = test.toGBK(str);

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

System.out.println();

String ascii = test.toASCII(str);

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

gbk = test.changeCharset(ascii,ChangeCharset.US_ASCII, ChangeCharset.GBK);

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

System.out.println();

String iso88591 = test.toISO_8859_1(str);

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

gbk = test.changeCharset(iso88591,ChangeCharset.ISO_8859_1, ChangeCharset.GBK);

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

System.out.println();

String utf8 = test.toUTF_8(str);

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

gbk = test.changeCharset(utf8,ChangeCharset.UTF_8, ChangeCharset.GBK);

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

System.out.println();

String utf16be = test.toUTF_16BE(str);

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

gbk = test.changeCharset(utf16be,ChangeCharset.UTF_16BE, ChangeCharset.GBK);

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

System.out.println();

String utf16le = test.toUTF_16LE(str);

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

gbk = test.changeCharset(utf16le,ChangeCharset.UTF_16LE, ChangeCharset.GBK);

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

System.out.println();

String utf16 = test.toUTF_16(str);

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

gbk = test.changeCharset(utf16,ChangeCharset.UTF_16LE, ChangeCharset.GBK);

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

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

System.out.println(s);

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值