Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1906155
  • 博文数量: 219
  • 博客积分: 8963
  • 博客等级: 中将
  • 技术积分: 2125
  • 用 户 组: 普通用户
  • 注册时间: 2005-10-19 12:48
个人简介

文章分类

全部博文(219)

文章存档

2021年(1)

2020年(3)

2015年(4)

2014年(5)

2012年(7)

2011年(37)

2010年(40)

2009年(22)

2008年(17)

2007年(48)

2006年(31)

2005年(4)

分类: Java

2011-02-25 09:59:33

 

import java.io.UnsupportedEncodingException;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;

public class HtmlEncode
{
    public static void main(String[] args)
    {
        String ex = "中国";
        String s = StringUtils.escape(ex);
        System.out.println(s); // \u4E2D\u56FD

        s = StringEscapeUtils.escapeHtml(ex);
        System.out.println(s); // 中国

        s = StringEscapeUtils.unescapeHtml(ex);
        System.out.println(s); // \u4E2D\u56FD

        for (byte b : ex.getBytes())
        {
            System.out.print(Integer.toHexString((int) b));
            // ffffffd6ffffffd0ffffffb9fffffffa

        }
        System.out.println();
        try
        {
            for (byte b : ex.getBytes("iso-8859-1"))
            {
                System.out.print(Integer.toHexString((int) b));
                // 3f3f

            }
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        System.out.println();
        try
        {
            for (byte b : ex.getBytes("utf-8"))
            {
                System.out.print(Integer.toHexString((int) b));
                // ffffffe4ffffffb8ffffffadffffffe5ffffff9bffffffbd

            }
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
    }
}


阅读(2367) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~