关于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。
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 = new String(b_gbk,"GBK");
String s_utf8 = new String(b_utf8,"UTF-8");
String s_iso88591 = new String(b_iso88591,"ISO8859-1");
通过打印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 = new String(str.getBytes("UTF-8"), "ISO-8859-1");
str = new String(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);
}
}
http://www.cnblogs.com/caowei/p/2013-12-11_request-response.html
http://blog.csdn.net/tianjf0514/article/details/7854624
关于java String类的getBytes(String charsetName)和String(byte[] bytes, String charsetName)的更多相关文章
- 用java String类的getBytes(String charsetName)和String(byte[] bytes, String charsetName)解决乱码问题
Java中String的数据是如何存储的,查看源代码就可以知道,String的数据是存储在char[] value这样一个成员变量中的,char类型的大小在java中是2个字节 我们还知道,现在普遍使 ...
- java 中String类的常用方法总结,带你玩转String类。
String类: String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.String类对象创建后不能修改,StringBuffer & St ...
- String(byte[] bytes, String charsetName)
String str = new String("时之沙"); byte bytes[] = str.getBytes("GBK"); byte byte2[] ...
- String 类的实现(3)引用计数实现String类
我们知道在C++中动态开辟空间时是用字符new和delete的.其中使用new test[N]方式开辟空间时实际上是开辟了(N*sizeof(test)+4)字节的空间.如图示其中保存N的值主要用于析 ...
- 深入理解Java常用类----String
Java中字符串的操作可谓是最常见的操作了,String这个类它封装了有关字符串操作的大部分方法,从构建一个字符串对象到对字符串的各种操作都封装在该类中,本篇我们通过阅读String类的源码 ...
- java.lang.String 类源码解读
String类定义实现了java.io.Serializable, Comparable<String>, CharSequence 三个接口:并且为final修饰. public fin ...
- Java之String类常用API
目录 Java之String类常用API char chatAt(int index) int length() char[] toCharArray() String(char value[]) S ...
- Java常用类(一)String类详解
前言 在我们开发中经常会用到很多的常用的工具类,这里做一个总结.他们有很多的方法都是我们经常要用到的.所以我们一定要把它好好的掌握起来! 一.String简介 1.1.String(字符串常量)概述 ...
- java常用类:1。包装类(以Integer类为例)2.String类 3.StringBuffer
包装类 把八大基本数据类型封装到一个类中,并提供属性和方法,更方便的操作基本数据类型. 包装类的出现并不是用于取代基本数据类型,也取代不了. 包装类位于java.lang包中. Number 类 N ...
随机推荐
- idea编辑器的使用
编辑器下载和安装就不说了,网上每次版本都更换得好快 ,发新版的人很多idea2019:https://pan.baidu.com/s/1zc1wkQLLVxbXSjy4ISN4aQ 提取码:cgah, ...
- 手摸手。完成一个H5 抽奖功能
要完成一个这样的抽奖功能 构思 奖励物品是通过接口获取的(img) 奖励结果是通过接口获取的(id) 抽奖的动画需要由慢到快再到慢 抽奖转动时间不能太短 抽奖结束需要回调 业务代码和功能代码要分离 先 ...
- 「Luogu」[JSOI2007]字符加密 解题报告
题面 思路: 作为一个后缀数组的初学者,当然首先想到的是后缀数组 把\(s\)这个串首尾相接,扩展为原来的两倍,就能按后缀数组的方法处理 证明: 神仙一眼就看出这是后缀的裸题,我这个蒟蒻想了半天想不出 ...
- FTP服务器红帽5.4搭建图文教程!!!
FTP服务器搭建 服务器的环境 红帽5.4 vm15 挂载光盘 mount mount -t iso9660 设备目录 /mnt 表示挂载 软件包安装 FTP服务器安装包命令: rpm -ivh /m ...
- 用TensorFlow做图像识别(python)
一.TensorFlow简介 TensorFlow是由谷歌开发的一套机器学习的工具,使用方法很简单,只需要输入训练数据位置,设定参数和优化方法等,TensorFlow就可以将优化结果显示出来,节省了很 ...
- Spring中PropertiesLoaderUtils应用
FileSystemResource fileSystemResource =new FileSystemResource("D:/home/conf/mail.properties&quo ...
- kettle连接oracle数据库报错,ORA-12505
报错信息: Error connecting to database: (using class oracle.jdbc.driver.OracleDriver) Listener refused t ...
- C#实现EXCEL表格转DataTable
C#代码实现把Excel文件转化为DataTable,根据Excel的文件后缀名不同,用不同的方法来进行实现,下面通过根据Excel文件的两种后缀名(*.xlsx和*.xls)分别来实现.获取文件后缀 ...
- Scala实践6
1 if表达式 Scala中if...else..表达式是有返回值的,如果if和else返回值类型不一样,则返回Any类型. scala> val a3=10 a3: Int = 10 sca ...
- js 极简获取表单 元素 !
let s =[]; $.each($('#formSearch input'),(m,n)=>{s.push(n)}); //示例获取表单所有 input 下滑线分割的 name 集合.set ...