`
TechBirds
  • 浏览: 82146 次
文章分类
社区版块
存档分类
最新评论

String(byte[] bytes, Charset charset) 和 getBytes() 使用

阅读更多

参考这篇文章:http://blog.csdn.net/maxracer/article/details/6075057

测试代码:

@Test
 	public void testBytes(){
 		//字节数
 		//中文:ISO:1 GBK:2 UTF-8:3   
 		//数字或字母: ISO:1 GBK:1 UTF-8:1
 		String username = "中";
 		try {
 			//得到指定编码的字节数组    字符串--->字节数组
 			byte[] u_iso=username.getBytes("ISO8859-1");
 			byte[] u_gbk=username.getBytes("GBK");
 			byte[] u_utf8=username.getBytes("utf-8");
 			System.out.println(u_iso.length);
 			System.out.println(u_gbk.length);
 			System.out.println(u_utf8.length);
 			//跟上面刚好是逆向的,字节数组---->字符串
 			String un_iso=new String(u_iso, "ISO8859-1");
 			String un_gbk=new String(u_gbk, "GBK");
 			String un_utf8=new String(u_utf8, "utf-8");
 			System.out.println(un_iso);
 			System.out.println(un_gbk);
 			System.out.println(un_utf8);		
 			//有时候必须是iso字符编码类型,那处理方式如下
 			String un_utf8_iso=new String(u_utf8, "ISO8859-1");		
 			//将iso编码的字符串进行还原
 			String un_iso_utf8=new String(un_utf8_iso.getBytes("ISO8859-1"),"UTF-8");
 			System.out.println(un_iso_utf8);				
 			
 		} catch (UnsupportedEncodingException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		}
 	}

测试结果:

1
2
3
?


中

从转载的文章摘:

乱码原因:为什么使用ISO8859-1编码再组合之后,无法还原"中"字呢,其实原因很简单,因为ISO8859-1编码的编码表中,根本就没有包含汉字字符,当然也就无法通过"中".getBytes("ISO8859-1");来得到正确的"中"字在ISO8859-1中的编码值了,所以再通过new String()来还原就无从谈起了.

有时候,为了让中文字符适应某些特殊要求(如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")来得到正确的中文汉字"中".这样就既保证了遵守协议规定、也支持中文.




分享到:
评论

相关推荐

    C# char[]与string byte[]与string之间的转换详解

    1、char[]与string之间的转换 //string 转换成 Char[] string str=hello; char[] arr=str.ToCharArray();...bytes = Encoding.UTF8.GetBytes(str); //string 转换成 byte[] (字符串是用哪种编码生成的byte[]

    C#_string_byte数组转换解析

    C# string byte数组转换解析 C# string byte数组转换实现的过程是什么呢?C# string byte数组间的转换需要注意什么呢?C# string byte数组间转换所涉及的方法是什么呢?让我们来看看具体的内容: C# string byte数组...

    StringAPI.java

    2.String和byte[]之间的转换 getBytes() Arrays工具类 : Arrays.toString(names) String类 String replace(char oldChar, char newChar) String replace(CharSequence target, CharSequence replacement) ...

    Java中的String类getBytes()方法详解与实例

    在本文中,我们学习了Java String类的getBytes()方法,它允许将字符串转换为字节数组,并且可以指定字符编码方式。通过实例和代码演示了使用平台默认字符编码和指定UTF-8、ISO-8859-1字符编码的情况。getBytes()方法...

    C# 字符串string和内存流MemoryStream及比特数组byte[]之间相互转换

    代码如下:(1)byte[] bt=System.Text.Encoding.Default.GetBytes(“字符串”); (2)byte[] bt=Convert.FromBase64String(“字符串”); 2.字符串转流 代码如下:(1)MemoryStream ms=new MemoryStream(System.Text....

    java初学者必读

    针对jsp和servlet: 解决办法: 第一: 在jsp页面加入: ; charset=gb2312" %> 或者在servlet里面 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, ...

    用四个字节十六进制数表示单精度浮点数1

    错误 1 不安全代码只会在使用 /unsafe 编译的情况下出现 E:\Visual Studio 2008\Projects\TEST\testOfFloat

    MyEclipse中文字过滤器

    byte[] bytes=input.getBytes("IS08859-1"); return new String(bytes,"GBK"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); ...

    JSP简单添加,查询功能代码

    本文实例讲述了JSP简单添加,...> bytesStr=str.getBytes( ISO-8859-1 ) ; return new String( bytesStr, gb2312 ) ; } catch( Exception ex) { return str ; } } } <%@ page contentType=text/html; charset=G

    Java版SMS4和Base64加密算法

    本资源是集合了SMS4和自定义的Base64加密算法。SMS4加密算法中提供了如下接口: private static byte[] encode16(byte[] plain, ...public static String decodeSMS4toString(byte[] cipher, byte[] key); Base64...

    java md5 jar

    byte bytes[] = md5.digest(); for(int i = 0; i < bytes.length; i++) { String s = Integer.toHexString(bytes[i] & 0xff); if(s.length()==1){ buf.append("0"); } buf.append(s...

    SpringMVC文件上传下载

    byte[] bytes = myfiles.getBytes(); FileCopyUtils.copy(bytes, new File("D:\\"+new Date().getTime()+fileName)); } return "/success"; } @RequestMapping(value="/download") public Response...

    DES对称分组密码系统的Java实现

    public static String bytes2Hex(byte[] data) { if (data==null) { return null; } else { int len = data.length; String str = ""; for (int i=0; i; i++) { if ((data[i]&0xFF)) str = str + "0" + ...

    BASE64Decoder包

    byte[] bt = key.getBytes(); 加密后字符串:String newKey=(new BASE64Encoder()).encodeBuffer(bt); 解密代码: 加密后的字符串:String newkey="*****"; byte[] bt = (new BASE64Decoder()).decodeBuffer(key); ...

    史上最全的java基础总结大全

    byte[] buf1 = str1.getBytes();//默认解码:Unicode,四个字节 //编码解码2:指定编码 String str2 = "你好"; byte[] buf2 = str2.getBytes("UTF-8");//指定解码:UTF-8,六个字节 //编码解码3:编码正确...

    MyEclipse注册机源码

    import java.io.*; public class test { ... byte abyte0[] = s.getBytes(); char ac[] = new char[s.length()]; int i = 0; for (int k = abyte0.length; i ; i++) { int j = abyte0[i];

    JNI函数使用

    传递字符串数组 参数传递 JNI调用 代码清单15-10 在Linux平台上调用C函数的例程——... texts[i]=new String(texts[i].getBytes("ISO8859-1"),"GBK"); System.out.print( texts[i] ); } System.out.println(); } }

    MD5加密和时间戳

    MD5加密和取得时间戳 public static string MD5(string input) { MD5 md5 = MD5CryptoServiceProvider.Create();... byte[] temp = md5.ComputeHash(Encoding.ASCII.GetBytes(input)); string byte2String = null;

    C# 中实现短信发送的类

    public static int SendSMS(string Number, string Message) { int returnValue = 0; IntPtr smsHandle = new IntPtr(0); // Set address structure byte[] smsatAddressType = BitConverter.GetBytes(SMSAT_...

    RSA加密解密(C#)实现

    public string RSAEncrypt(string xmlPublicKey, byte[] EncryptString) { try { byte[] CypherTextBArray; string Result; System.Security.Cryptography.RSACryptoServiceProvider rsa = new ...

Global site tag (gtag.js) - Google Analytics