黑马程序员:查看Unicode码,GB2312码 及System.out.println()的含义

来源:互联网 发布:大数据与统计新思维 编辑:程序博客网 时间:2024/06/06 03:59


/**
 * @author cui
 *
 */
public class CharCode {

 /**
  * @param args
  */
 public static void main(String[] args) {
    String charChina = "中国";
  for (int i = 0; i < charChina.length(); i++) {
   /* 查看Unicode码,字符编码通常为16进制 */
   System.out.println(Integer.toHexString((int) charChina.charAt(i)));
   /* Integer.toHexString()转化为16进制*/
  }
  /* 查看国标码 */
  try {
   byte[] buf = charChina.getBytes("gb2312");            (1 )
   for (int i = 0; i < buf.length; i++) {
    System.out.println(Integer.toHexString(buf[i]));
   }
   /* 将数组写入显示器 */
   for (int i = 0; i < buf.length; i++) {
    System.out.write(buf[i]);               (2)
   }
   /* 使用换行 结束缓冲 write()写入的如果是字节数组会自动结束缓冲 */
   System.out.println();
   System.out.println("中国");

   /* 

    *System.out.println("中国"); 可直接输出中国。
    * 实际上System.out是PrintStream的一个实例对象 PrintStream.println(String str);
    * 这个方法先将字符串转变为 GB2312 后用 write 方法写入显示器 完成了上面(1)(2)的过程
    */
  } catch (Exception e) {

         e.printStackTrace();
    }
 }
}

原创粉丝点击