charsetname java_关于java String类的getBytes(String charsetName)和String(byte[] bytes, String charsetName...

public byte[] getBytes(Charset charset)

Encodes this String into a sequence of bytes using the given charset, storing the result into a new byte array.

This method always replaces malformed-input and unmappable-character sequences with this charset's default replacement byte array. The CharsetEncoder class should be used when more control over the encoding process is required.

使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。

当此字符串不能使用给定的字符集编码时,此方法的行为没有指定。如果需要对编码过程进行更多控制,则应该使用 CharsetEncoder 类。

在Java中,String.getBytes(String decode)方法会根据指定的decode编码返回某字符串在该编码下的byte数组表示,如

byte[] b_gbk = "中".getBytes("GBK");byte[] b_utf8 = "中".getBytes("UTF-8");byte[] b_iso88591 = "中".getBytes("ISO8859-1");

将分别返回“中”这个汉字在GBK、UTF-8和ISO8859-1编码下的byte数组表示。

此时b_gbk的长度为2,b_utf8的长度为3,b_iso88591的长度为1。

4c1923d46d540dd5538d8b404db883a8.png

public String(byte[] bytes, Charset charset)

Constructs a new String by decoding the specified array of bytes using the specified charset. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.

This method always replaces malformed-input and unmappable-character sequences with this charset's default replacement string. The CharsetDecoder class should be used when more control over the decoding process is required.

通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String。新 String 的长度是字符集的函数,因此可能不等于 byte 数组的长度。

此方法总是使用此字符集的默认替代字符串替代错误输入和不可映射字符序列。如果需要对解码过程进行更多控制,则应该使用 CharsetDecoder 类。

而与getBytes相对的,可以通过new String(byte[], decode)的方式来还原这个“中”字。

这个new String(byte[], decode)实际是使用decode指定的编码来将byte[]解析成UNICODE字符串。(按照什么存就得按照什么解)

String s_gbk = newString(b_gbk,"GBK");String s_utf8 = newString(b_utf8,"UTF-8");String s_iso88591 = newString(b_iso88591,"ISO8859-1");

f3b4808e16fad70dab0f0606ee84a674.png

通过打印s_gbk、s_utf8和s_iso88591,会发现,s_gbk和s_utf8都是“中”,而只有s_iso88591是?,为什么使用ISO8859-1编码再组合之后,无法还原“中”字呢,其实原因很简单,因为ISO8859-1编码的编码表中,根本就没有包含汉字字符,当然也就无法通过"中".getBytes("ISO8859-1");来得到正确的“中”字在ISO8859-1中的编码值了,所以再通过new String()来还原就无从谈起了。

因此,通过String.getBytes(String decode)方法来得到byte[]时,一定要确定decode的编码表中确实存在String表示的码值,这样得到的byte[]数组才能正确被还原。

有时候,为了让中文字符适应某些特殊要求(如http header头要求其内容必须为iso8859-1编码),可能会通过将中文字符按照字节方式来编码的情况,如

String s_iso88591 = new String("中".getBytes("UTF-8"),"ISO8859-1"),

这样得到的s_iso8859-1字符串实际是三个在 ISO8859-1中的字符,在将这些字符传递到目的地后,

目的地程序再通过相反的方式String s_utf8 = new String(s_iso88591.getBytes("ISO8859-1"),"UTF-8")来得到正确的中文汉字“中”。这样就既保证了遵守协 议规定、也支持中文。

String str = "中国";String iso88591 = newString(str.getBytes("UTF-8"),"ISO-8859-1");str = newString(iso88591.getBytes("ISO-8859-1"),"UTF-8");System.out.println(str);

import java.io.UnsupportedEncodingException;

/**

* Created by N3verL4nd on 2017/1/2.

*/

public class HelloWorld

{

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

String str=new String("我爱天安门");

byte by_gbk[]=str.getBytes("GBK");

String str_gbk=new String(by_gbk,"GBK");

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

String str_utf8=new String(by_gbk,"UTF-8");

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

System.out.println("----------------------");

byte by_utf8[]=str.getBytes("UTF-8");

String str2_gbk=new String(by_utf8,"GBK");

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

String str2_utf8=new String(by_utf8,"UTF-8");

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

}

}

fd95f27c49abdffca21dd96ed5601b55.png

http://www.cnblogs.com/caowei/p/2013-12-11_request-response.html

http://blog.csdn.net/tianjf0514/article/details/7854624

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码是一个使用 FastJson 序列化和反序列化对象的 Redis 序列化器。下面是对这段代码的注释: ```java public class FastJsonRedisSerializer<T> implements RedisSerializer<T> { public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8; // 默认字符集为 UTF-8 private final Class<T> clazz; // 要序列化和反序列化的对象型 static { ParserConfig.getGlobalInstance().setAutoTypeSupport(true); // 设置 FastJson 全局配置,开启自动型支持 } public FastJsonRedisSerializer(Class<T> clazz) { super(); this.clazz = clazz; // 初始化对象型 } @Override public byte[] serialize(T t) throws SerializationException { if (t == null) { return new byte[0]; // 如果对象为空,返回空字节数组 } return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET); // 使用 FastJson 将对象转换为 JSON 字符串,并转换为字节数组 } @Override public T deserialize(byte[] bytes) throws SerializationException { if (bytes == null || bytes.length <= 0) { return null; // 如果字节数组为空,返回空对象 } String str = new String(bytes, DEFAULT_CHARSET); // 将字节数组转换为字符串 return JSON.parseObject(str, clazz); // 使用 FastJson 将字符串解析为指定的对象型 } protected JavaType getJavaType(Class<?> clazz) { return TypeFactory.defaultInstance().constructType(clazz); // 获取 JavaType 对象,用于序列化和反序列化 } } ``` 这个实现了 RedisSerializer 接口,并使用 FastJson 库来进行对象的序列化和反序列化操作。它通过构造方法传入要序列化和反序列化的对象型,并提供了 `serialize` 和 `deserialize` 方法来实现对象和字节数组之间的转换。在静态代码块中,设置了 FastJson 的全局配置,开启自动型支持。该还提供了一个受保护的方法 `getJavaType`,用于获取 JavaType 对象,用于序列化和反序列化操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值