Java Charset

举报
福州司马懿 发表于 2021/11/19 04:07:33 2021/11/19
【摘要】 package com.demo.test; import java.nio.charset.Charset;import java.util.Arrays; /**(1)在中国,字符串转化默认使用GBK编码(2)转化表 英文(L) 中文(L) 混合(英文L1 + 中文L2)DEFAULT(GBK) L 2...


  
  1. package com.demo.test;
  2. import java.nio.charset.Charset;
  3. import java.util.Arrays;
  4. /**
  5. (1)在中国,字符串转化默认使用GBK编码
  6. (2)转化表
  7. 英文(L) 中文(L) 混合(英文L1 + 中文L2)
  8. DEFAULT(GBK) L 2*L L1 + 2*L2
  9. ISO8859-1 L L L1 + L2
  10. ASCII L L L1 + L2
  11. GBK L 2*L L1 + 2*L2
  12. UTF-8 L 3*L L1 + 3*L2
  13. UNICODE 2*L+2 2*L+2 2*(L1 + L2)+2
  14. */
  15. public class CharsetDemo {
  16. public static void main(String[] args) {
  17. System.out.println("ASCII " + Charset.isSupported("ASCII"));//true
  18. System.out.println("Shift_JIS " + Charset.isSupported("Shift_JIS"));//true
  19. System.out.println("ISO-8859-1 " + Charset.isSupported("ISO-8859-1"));//true
  20. System.out.println("I-S-O-8859-1 " + Charset.isSupported("I-S-O-8859-1"));//false
  21. System.out.println("ISO8859-1 " + Charset.isSupported("ISO8859-1"));//true
  22. System.out.println("ISO8859_1 " + Charset.isSupported("ISO8859_1"));//true
  23. //error: java.nio.charset.IllegalCharsetNameException
  24. // System.out.println("ISO8859 1 " + Charset.isSupported("ISO8859 1"));
  25. //ISO8859必须有第一版的标识否则不合法
  26. System.out.println("ISO8859 " + Charset.isSupported("ISO8859"));//false
  27. //ISO/IEC8859-1,又称Latin-1或“ 西欧语言”,但这里用Latin-1却是不合法的
  28. System.out.println("Latin-1 " + Charset.isSupported("Latin-1"));//false
  29. System.out.println("UTF8 " + Charset.isSupported("UTF8"));//true
  30. System.out.println("UTF-8 " + Charset.isSupported("UTF-8"));//true
  31. System.out.println("UTF_8 " + Charset.isSupported("UTF_8"));//false
  32. //CharsetName跟大小写无关
  33. System.out.println("Unicode " + Charset.isSupported("Unicode"));//true
  34. System.out.println("uNiCoDe " + Charset.isSupported("uNiCoDe"));//true
  35. String str1 = "hello world";//11位
  36. System.out.println(Arrays.toString(str1.toCharArray()));
  37. System.out.println(Arrays.toString(toCharBytes(str1)));
  38. System.out.println(Arrays.toString(str1.getBytes()));//11位,[104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
  39. System.out.println(Arrays.toString(str1.getBytes(Charset.forName("ISO8859-1"))));//11位,[104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
  40. System.out.println(Arrays.toString(str1.getBytes(Charset.forName("ASCII"))));//11位,[104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
  41. System.out.println(Arrays.toString(str1.getBytes(Charset.forName("GBK"))));//11位,[104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
  42. System.out.println(Arrays.toString(str1.getBytes(Charset.forName("UTF-8"))));//11位,[104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]
  43. System.out.println(Arrays.toString(str1.getBytes(Charset.forName("Unicode"))));//24位,[-2, -1, 0, 104, 0, 101, 0, 108, 0, 108, 0, 111, 0, 32, 0, 119, 0, 111, 0, 114, 0, 108, 0, 100]
  44. System.out.println();
  45. String str2 = "可惜不是你";//5位
  46. System.out.println(Arrays.toString(str2.toCharArray()));
  47. System.out.println(Arrays.toString(toCharBytes(str2)));
  48. System.out.println(Arrays.toString(str2.getBytes()));//10位,[-65, -55, -49, -89, -78, -69, -54, -57, -60, -29]
  49. System.out.println(Arrays.toString(str2.getBytes(Charset.forName("ISO8859-1"))));//5位,[63, 63, 63, 63, 63]
  50. System.out.println(Arrays.toString(str2.getBytes(Charset.forName("ASCII"))));//5位,[63, 63, 63, 63, 63]
  51. System.out.println(Arrays.toString(str2.getBytes(Charset.forName("GBK"))));//10位,[-65, -55, -49, -89, -78, -69, -54, -57, -60, -29]
  52. System.out.println(Arrays.toString(str2.getBytes(Charset.forName("UTF-8"))));//15位,[-27, -113, -81, -26, -125, -100, -28, -72, -115, -26, -104, -81, -28, -67, -96]
  53. System.out.println(Arrays.toString(str2.getBytes(Charset.forName("Unicode"))));//12位,[-2, -1, 83, -17, 96, -36, 78, 13, 102, 47, 79, 96]
  54. System.out.println();
  55. String str3 = "love!我爱你哟!";//5+5位
  56. System.out.println(Arrays.toString(str3.toCharArray()));
  57. System.out.println(Arrays.toString(toCharBytes(str3)));
  58. System.out.println(Arrays.toString(str3.getBytes()));//15位,[108, 111, 118, 101, 33, -50, -46, -80, -82, -60, -29, -45, -76, -93, -95]
  59. System.out.println(Arrays.toString(str3.getBytes(Charset.forName("ISO8859-1"))));//10位,[108, 111, 118, 101, 33, 63, 63, 63, 63, 63]
  60. System.out.println(Arrays.toString(str3.getBytes(Charset.forName("ASCII"))));//10位,[108, 111, 118, 101, 33, 63, 63, 63, 63, 63]
  61. System.out.println(Arrays.toString(str3.getBytes(Charset.forName("GBK"))));//15位,[108, 111, 118, 101, 33, -50, -46, -80, -82, -60, -29, -45, -76, -93, -95]
  62. System.out.println(Arrays.toString(str3.getBytes(Charset.forName("UTF-8"))));//20位,[108, 111, 118, 101, 33, -26, -120, -111, -25, -120, -79, -28, -67, -96, -27, -109, -97, -17, -68, -127]
  63. System.out.println(Arrays.toString(str3.getBytes(Charset.forName("Unicode"))));//22位,[-2, -1, 0, 108, 0, 111, 0, 118, 0, 101, 0, 33, 98, 17, 114, 49, 79, 96, 84, -33, -1, 1]
  64. }
  65. public static byte[] toCharBytes(String str) {
  66. char[] charArr = str.toCharArray();
  67. byte[] byteArr = new byte[charArr.length*2];
  68. for(int i=0;i<charArr.length;i++) {
  69. //当字符是英文时,该位的编码与ASCII和ISO8859-1均相同
  70. byteArr[2*i] = (byte)(charArr[i] & 0xFF);
  71. //当字符是英文的时候,高位是0;如果字符是中文,那么高位不为0。并且该编码也与GBK、UTF8、Unicode都不同
  72. byteArr[2*i+1] = (byte)((charArr[i] & 0xFF00) >>> 8);
  73. }
  74. return byteArr;
  75. }
  76. }

文章来源: blog.csdn.net,作者:福州-司马懿,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/chy555chy/article/details/52045782

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。