关于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 ...
随机推荐
- 毕业两年半,入手人生第一款macbook pro
当程序员入手第一款macbook 大家好,我是灰大狼,你们可以叫我灰狼.大狼.甚至是小灰灰. 接下来我主要跟大家分享下作为程序员的我,刚入手一款mac的使用心得. 背景 做程序员三年了,一直用的都是w ...
- vue iView table中render渲染
<template> <div class="srm-page"> <el-form ref="form" :model=&quo ...
- 「UVA12004」 Bubble Sort 解题报告
UVA12004 Bubble Sort Check the following code which counts the number of swaps of bubble sort. int f ...
- Shell水平测试-想学习Shell的童鞋必选必看文章
[SHELL水平测试] [OVERVIEW 篇] 有很多种 shell, 你熟悉几种? 各个 shell 的 home page 在那里? 为什么说 zsh 是目前为止功能最为强大的 shell. 为 ...
- 递推 dp - 求有多少个序列符合题意
题目描述 小美有一个由n个元素组成的序列{a1,a2,a3,...,an},她想知道其中有多少个子序列{ap1,ap2,...,apm}(1 ≤ m ≤ n, 1 ≤ p1 < p2 , ...
- AI漫谈:我们距离实现《庆余年》里的五竹叔机器人还有多远?
(警告: 本文包含少量剧透内容,请酌情阅读) 五竹叔是机器人吗? 看过庆余年的朋友,一定对五竹叔印象深刻,外表英俊潇洒,一袭黑衣加黑布条蒙眼,充满神秘侠客气息.五竹叔不但神秘,而且言行举止常常很 ...
- unittest模块使用方法
unittest模块常用属性 1. unittest.TestCase类:所有的测试用例类继承的基类 定义一个测试用例类,需要继承TestCase,比如: class BaiduTest(unitte ...
- Window初始化Git环境
安装Git 去到官网下载地址,找到自己电脑的对应版本,下载安装就好啦,这里就不一一说明了 https://git-scm.com/download/win 初始化Git环境 第一步:打开git-bas ...
- python中常⽤的excel模块库
python中常用的excel模块库&安装方法 openpyxl openpyxl是⼀个Python库,用于读取/写⼊Excel 2010 xlsx / xlsm / xltx / xltm⽂ ...
- Django 滑动验证
极验官网:https://www.geetest.com/ 文档: https://docs.geetest.com/ 查看 行为验证的部署文档