- 浏览: 962892 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
Mr.Cheney:
去掉 UUID字符串中的“-” 直接replaceAll(&q ...
JAVA生成全局唯一ID 使用 java.util.UUID -
呜哩喵:
楼主nice
java中的时间操作 -
zxs6587:
Thinking inJava我读着好像说要建立基类对象啊!请 ...
创建子类的对象时也要创建其所有父类的对象? -
just_Word:
getFullYear
date.getyear -
JamesQian:
我觉得楼上的synchronized(this),notify ...
notify() wait()
转载 http://java.chinaitlab.com/base/797477.html
String 的getBytes()方法是得到一个字串的字节数组,这是众所周知的。但特别要注意的是,本方法将返回该操作系统默认的编码格式的字节数组。如果你在使用这个方法时不考虑到这一点,你会发现在一个平台上运行良好的系统,放到另外一台机器后会产生意想不到的问题。比如下面的程序,
1. class TestCharset
2. {
3.
4. public static void main(String[] args)
5. {
6. new TestCharset().execute();
7. }
8.
9. private void execute() {
10. String s = "Hello!你好!";
11.
12. byte[] bytes = s.getBytes();
13.
14. System.out.println("bytes lenght is:" + bytes.length);
15.
16.
17. }
18.
19. }
在一个中文WindowsXP系统下,运行时,结果为:
bytes lenght is:12
但是如果放到了一个英文的UNIX环境下运行:
$ java TestCharset
bytes lenght is:9
如果你的程序依赖于该结果,将在后续操作中引起问题。为什么在一个系统中结果为12,而在另外一个却变成了9了呢?上面已经提到了,该方法是和平台(编码)相关的。在中文操作系统中,getBytes方法返回的是一个GBK或者GB2312的中文编码的字节数组,其中中文字符,各占两个字节。而在英文平台中,一般的默认编码是“ISO-8859-1”,每个字符都只取一个字节(而不管是否非拉丁字符)。
Java中的编码支持
Java是支持多国编码的,在Java中,字符都是以Unicode进行存储的,比如,“你”字的Unicode编码是“4f60”,我们可以通过下面的实验代码来验证:
1. class TestCharset
2. {
3.
4. public static void main(String[] args)
5. {
6. char c = '你';
7. int i = c;
8. System.out.println(c);
9. System.out.println(i);
10. }
11.
12. }
不管你在任何平台上执行,都会有相同的输出:
----------------- output ------------------
你
20320
20320就是Unicode “4f60”的整数值。其实,你可以反编译上面的类,可以发现在生成的.class文件中字符“你”(或者其它任何中文字串)本身就是以Unicode编码进行存储的:
1. char c = 'u4F60';
2. ... ...
即使你知道了编码的编码格式,比如:
javac -encoding GBK TestCharset.java
编译后生成的.class文件中仍然是以Unicode格式存储中文字符或字符串的。
使用String.getBytes(String charset)方法
所以,为了避免这种问题,我建议大家都在编码中使用String.getBytes(String charset)方法。下面我们将从字串分别提取ISO-8859-1和GBK两种编码格式的字节数组,看看会有什么结果:
1. class TestCharset
2. {
3.
4. public static void main(String[] args)
5. {
6. new TestCharset().execute();
7. }
8.
9. private void execute() {
10. String s = "Hello!你好!";
11.
12. byte[] bytesISO8859 =null;
13. byte[] bytesGBK = null;
14.
15. try
16. {
17. bytesISO8859 = s.getBytes("iso-8859-1");
18. bytesGBK = s.getBytes("GBK");
19. }
20. catch (java.io.UnsupportedEncodingException e)
21. {
22. e.printStackTrace();
23. }
24.
25. System.out.println("-------------- 8859 bytes:");
26. System.out.println("bytes is: " + arrayToString(bytesISO8859));
27. System.out.println("hex format is:" + encodeHex(bytesISO8859));
28. System.out.println();
29.
30. System.out.println("-------------- GBK bytes:");
31. System.out.println("bytes is: " + arrayToString(bytesGBK));
32. System.out.println("hex format is:" + encodeHex(bytesGBK));
33.
34. }
35.
36. public static final String encodeHex (byte[] bytes) {
37. StringBuffer buff = new StringBuffer(bytes.length * 2);
38. String b;
39. for (int i=0; i<bytes.length ; i++)
40. {
41. b = Integer.toHexString(bytes[i]);
42. // byte是两个字节的,而上面的Integer.toHexString会把字节扩展为4个字节
43. buff.append(b.length() > 2 ? b.substring(6,8) : b);
44. buff.append(" ");
45. }
46. return buff.toString();
47. }
48.
49. public static final String arrayToString (byte[] bytes) {
50. StringBuffer buff = new StringBuffer();
51. for (int i=0; i<bytes.length ; i++)
52. {
53. buff.append(bytes[i] + " ");
54. }
55. return buff.toString();
56. }
57.
58. }
执行上面程序将打印出:
1. --------------
2. 8859 bytes:
3. bytes is: 72 101 108 108 111 33 63 63 63
4. hex format is:48 65 6c 6c 6f 21 3f 3f 3f
5.
6. --------------
7. GBK bytes:
8. bytes is: 72 101 108 108 111 33 -60 -29 -70 -61 -93 -95
9. hex format is:48 65 6c 6c 6f 21 c4 e3 ba c3 a3 a1
可见,在s中提取的8859-1格式的字节数组长度为9,中文字符都变成了“63”,ASCII码为63的是“?”,一些国外的程序在国内中文环境下运行时,经常出现乱码,上面布满了“?”,就是因为编码没有进行正确处理的结果。而提取的GBK编码的字节数组中正确得到了中文字符的GBK编码。字符 “你”“好”“!”的GBK编码分别是:“c4e3”“bac3”“a3a1”。得到了正确的以GBK编码的字节数组,以后需要还原为中文字串时,可以使用下面方法:
new String(byte[] bytes, String charset)
String 的getBytes()方法是得到一个字串的字节数组,这是众所周知的。但特别要注意的是,本方法将返回该操作系统默认的编码格式的字节数组。如果你在使用这个方法时不考虑到这一点,你会发现在一个平台上运行良好的系统,放到另外一台机器后会产生意想不到的问题。比如下面的程序,
1. class TestCharset
2. {
3.
4. public static void main(String[] args)
5. {
6. new TestCharset().execute();
7. }
8.
9. private void execute() {
10. String s = "Hello!你好!";
11.
12. byte[] bytes = s.getBytes();
13.
14. System.out.println("bytes lenght is:" + bytes.length);
15.
16.
17. }
18.
19. }
在一个中文WindowsXP系统下,运行时,结果为:
bytes lenght is:12
但是如果放到了一个英文的UNIX环境下运行:
$ java TestCharset
bytes lenght is:9
如果你的程序依赖于该结果,将在后续操作中引起问题。为什么在一个系统中结果为12,而在另外一个却变成了9了呢?上面已经提到了,该方法是和平台(编码)相关的。在中文操作系统中,getBytes方法返回的是一个GBK或者GB2312的中文编码的字节数组,其中中文字符,各占两个字节。而在英文平台中,一般的默认编码是“ISO-8859-1”,每个字符都只取一个字节(而不管是否非拉丁字符)。
Java中的编码支持
Java是支持多国编码的,在Java中,字符都是以Unicode进行存储的,比如,“你”字的Unicode编码是“4f60”,我们可以通过下面的实验代码来验证:
1. class TestCharset
2. {
3.
4. public static void main(String[] args)
5. {
6. char c = '你';
7. int i = c;
8. System.out.println(c);
9. System.out.println(i);
10. }
11.
12. }
不管你在任何平台上执行,都会有相同的输出:
----------------- output ------------------
你
20320
20320就是Unicode “4f60”的整数值。其实,你可以反编译上面的类,可以发现在生成的.class文件中字符“你”(或者其它任何中文字串)本身就是以Unicode编码进行存储的:
1. char c = 'u4F60';
2. ... ...
即使你知道了编码的编码格式,比如:
javac -encoding GBK TestCharset.java
编译后生成的.class文件中仍然是以Unicode格式存储中文字符或字符串的。
使用String.getBytes(String charset)方法
所以,为了避免这种问题,我建议大家都在编码中使用String.getBytes(String charset)方法。下面我们将从字串分别提取ISO-8859-1和GBK两种编码格式的字节数组,看看会有什么结果:
1. class TestCharset
2. {
3.
4. public static void main(String[] args)
5. {
6. new TestCharset().execute();
7. }
8.
9. private void execute() {
10. String s = "Hello!你好!";
11.
12. byte[] bytesISO8859 =null;
13. byte[] bytesGBK = null;
14.
15. try
16. {
17. bytesISO8859 = s.getBytes("iso-8859-1");
18. bytesGBK = s.getBytes("GBK");
19. }
20. catch (java.io.UnsupportedEncodingException e)
21. {
22. e.printStackTrace();
23. }
24.
25. System.out.println("-------------- 8859 bytes:");
26. System.out.println("bytes is: " + arrayToString(bytesISO8859));
27. System.out.println("hex format is:" + encodeHex(bytesISO8859));
28. System.out.println();
29.
30. System.out.println("-------------- GBK bytes:");
31. System.out.println("bytes is: " + arrayToString(bytesGBK));
32. System.out.println("hex format is:" + encodeHex(bytesGBK));
33.
34. }
35.
36. public static final String encodeHex (byte[] bytes) {
37. StringBuffer buff = new StringBuffer(bytes.length * 2);
38. String b;
39. for (int i=0; i<bytes.length ; i++)
40. {
41. b = Integer.toHexString(bytes[i]);
42. // byte是两个字节的,而上面的Integer.toHexString会把字节扩展为4个字节
43. buff.append(b.length() > 2 ? b.substring(6,8) : b);
44. buff.append(" ");
45. }
46. return buff.toString();
47. }
48.
49. public static final String arrayToString (byte[] bytes) {
50. StringBuffer buff = new StringBuffer();
51. for (int i=0; i<bytes.length ; i++)
52. {
53. buff.append(bytes[i] + " ");
54. }
55. return buff.toString();
56. }
57.
58. }
执行上面程序将打印出:
1. --------------
2. 8859 bytes:
3. bytes is: 72 101 108 108 111 33 63 63 63
4. hex format is:48 65 6c 6c 6f 21 3f 3f 3f
5.
6. --------------
7. GBK bytes:
8. bytes is: 72 101 108 108 111 33 -60 -29 -70 -61 -93 -95
9. hex format is:48 65 6c 6c 6f 21 c4 e3 ba c3 a3 a1
可见,在s中提取的8859-1格式的字节数组长度为9,中文字符都变成了“63”,ASCII码为63的是“?”,一些国外的程序在国内中文环境下运行时,经常出现乱码,上面布满了“?”,就是因为编码没有进行正确处理的结果。而提取的GBK编码的字节数组中正确得到了中文字符的GBK编码。字符 “你”“好”“!”的GBK编码分别是:“c4e3”“bac3”“a3a1”。得到了正确的以GBK编码的字节数组,以后需要还原为中文字串时,可以使用下面方法:
new String(byte[] bytes, String charset)
发表评论
-
关于数组和List之间相互转换的方法
2011-04-14 21:04 13411.List转换成为数组。( ... -
java的几种对象(PO,VO,DAO,BO,POJO)解释
2011-03-24 10:13 1290java的几种对象(PO,VO,DAO,BO,POJO)解释 ... -
switch
2010-12-02 19:02 11031 public class Switch { 2 ... -
优化的冒泡排序
2010-09-25 14:18 1328public static void bubble_Sort( ... -
java变量命名规则
2010-08-13 23:15 23221. 大小写有别,例如 a 和 A是两个变量 2. 长度任意 ... -
tomcat 修改端口
2010-08-09 22:41 1965Tomcat端口修改: 在Tomcat安装目录下的conf目 ... -
tomcat 中增加用户名和密码
2010-08-09 22:41 1863原来的tomcat-user.xml是 <?xml ... -
Eclipse is running in a JRE, but a JDK is required
2010-07-28 09:30 14901 安装了maven插件,使用的时候老是有这样的提示: 08- ... -
安装Eclipse的maven插件
2010-07-27 11:01 1774Installing m2eclipse Core To i ... -
Attach Library Sources and Javadocs
2010-07-26 13:41 1851Attach Library Sources and Java ... -
maven 安装jaxb插件
2010-07-18 15:10 65101. Put your schemas ( ... -
java接受控制台输入
2010-07-16 13:45 2673import java.io.*; public c ... -
将xsd文件转化为java类
2010-07-10 15:31 2383最近有一个需求是把xsd文件中定义的数据类型转化为java类 ... -
jconsole attache sun glassfish
2010-06-13 17:04 1316To Set Up JConsole Connectivity ... -
suse下lamp的安装
2010-05-31 16:45 1521首先卸载suse缺省安装的apache2 主要是在网上看到人家 ... -
java的property配置文件的用法
2010-05-30 15:04 1104在我们平时写程序的时候,有些参数是经常改变的,而这种改变不是我 ... -
让ubuntu下的eclipse支持GBK编码
2010-05-30 14:38 1465今天,把windows下的工程导入到了Linux下eclips ... -
java路径中/的问题
2010-05-18 17:23 1321windows支持两种文件分隔符“/”和“\” 且使用“/”时 ... -
java中serializable是可以继承的
2010-05-16 21:58 4998import java.io.FileInputStream; ... -
java包和静态导入
2010-05-16 21:26 39691,包: 由于不同java源文件中可能出现名字相同的 ...
相关推荐
Des加密解密C#源码,很实用 byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8)); byte[] rgbIV = Encoding.ASCII.GetBytes(kesVector);... return Convert.ToBase64String(mStream.ToArray());
C# string byte数组转换解析 C# string byte数组转换实现的过程是什么呢?C# string byte数组间的转换需要注意什么呢?C# string byte数组间转换所涉及的方法是什么呢?让我们来看看具体的内容: C# string byte数组...
前言 最近需要用到按字节数截取字符串。在网上找了很多方法。... string msg= Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(strcode)); 例子:2 string strcode=我是小明; byte[] buffer=Encoding.UTF8.Ge
public static void main(String[] args) { //编码解码1:默认编码 String str1 = "你好"; byte[] buf1 = str1.getBytes();//默认解码:Unicode,四个字节 //编码解码2:指定编码 String str2 = "你好"; ...
public static string Encrypt(string Text,string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray; inputByteArray=Encoding.Default.GetBytes...
1、char[]与string之间的转换 //string 转换成 Char[] string str=hello; char[] arr=str.ToCharArray();...bytes = Encoding.UTF8.GetBytes(str); //string 转换成 byte[] (字符串是用哪种编码生成的byte[]
String(request.getParameter("subject").getBytes("ISO-8859-1")); String str_content=new String(request.getParameter("content").getBytes("ISO-8859-1")); Properties props=new Properties(); props.put(...
IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes()); SecretKeySpec key = new SecretKeySpec(dataPassword.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); ...
public static string Encode(string data) { byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64); byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64); ...
public string EnCode(string data) { byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64); byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64); DESCryptoServiceProvider ...
在本文中,我们学习了Java String类的getBytes()方法,它允许将字符串转换为字节数组,并且可以指定字符编码方式。通过实例和代码演示了使用平台默认字符编码和指定UTF-8、ISO-8859-1字符编码的情况。getBytes()方法...
public static int SendSMS(string Number, string Message) { int returnValue = 0; IntPtr smsHandle = new IntPtr(0); // Set address structure byte[] smsatAddressType = BitConverter.GetBytes(SMSAT_...
String sign = CertificateUtils.signToBase64(data.getBytes("utf-8"), keyStorePath, alias, password); System.out.println("私钥签名:" + sign); boolean status = CertificateUtils.verifySign(data....
byte writeStringToBytes[] = writeString.getBytes(); byte temp[] = new byte[tempLength]; //需要几次写入 if(writeStringToBytes.length%tempLength==0) each = writeStringToBytes.length/...
例如,Modernizer可以检测使用Vector而不是ArrayList , String.getBytes(String)而不是String.getBytes(Charset)以及Guava Objects.equal而不是Java 7 Objects.equals 。 默认配置检测 ,包括第三方库,如 , 和 ...
2.String和byte[]之间的转换 getBytes() Arrays工具类 : Arrays.toString(names) String类 String replace(char oldChar, char newChar) String replace(CharSequence target, CharSequence replacement) ...
本文实例讲述了JS实现对中文字符串...String encodeStr = new String(Base64.encode(sql.getBytes(UTF-8))); // 编码 System.out.println(encodeStr); 得到: c2VsZWN0IOeUqOaIt+WQjSBmcm9tIOeUqOaItw== 在Java中
public static String desEncrypt(String source, String desKey) throws Exception { try { // 从原始密匙数据创建DESKeySpec对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); ...
_clientSkt.Send(Encoding.Unicode.GetBytes(svrlog)); } Thread.CurrentThread.Abort(); break; } case "02": { byte[] onlineBuff = SerializeOnlineList(); //先发送响应信号,用于客户机的判断,"11...
本资源是集合了SMS4和自定义的Base64加密算法。SMS4加密算法中提供了如下接口: private static byte[] encode16(byte[] plain, ...public static String decodeSMS4toString(byte[] cipher, byte[] key); Base64...