java char字符转编码_关于字符编码工具类CharTools实现字符编码转换、Utf8URL网址中文编码解码代码示例...

一、前言

通过java.net.URLDecoder定义CharTools字符编码工具类,实现转换编码 ISO-8859-1到GB2312方法ISO2GB转换、转换编码 GB2312到ISO-8859-1方法GB2ISO转换、中文网址Utf8URL编码/解码Utf8URLencode/Utf8URLdecode、判断是否Utf8Url编码isUtf8Url等方法代码示例。

二、代码示例package com.xwood.util;@b@@b@import java.io.UnsupportedEncodingException;@b@import java.net.URLDecoder;@b@@b@/**@b@* 

Title:字符编码工具类 

@b@* 

Description:  

@b@* 

Copyright:  Copyright (c) 2007

@b@* 

Company:  

@b@* @version 1.0@b@*/@b@@b@public class  CharTools{@b@@b@  /**@b@   * 转换编码 ISO-8859-1到GB2312@b@   * @param text@b@   * @return@b@   */@b@  public static final String ISO2GB(String text){@b@    String result = "";@b@    try {@b@      result = new String(text.getBytes("ISO-8859-1"), "GB2312");@b@    }catch (UnsupportedEncodingException ex) {@b@      result = ex.toString();@b@    }@b@    return result;@b@  }@b@@b@  /**@b@   * 转换编码 GB2312到ISO-8859-1@b@   * @param text@b@   * @return@b@   */@b@  public static final String GB2ISO(String text) {@b@    String result = "";@b@    try {@b@      result = new String(text.getBytes("GB2312"), "ISO-8859-1");@b@    }catch (UnsupportedEncodingException ex) {@b@      ex.printStackTrace();@b@    }@b@    return result;@b@  }@b@  @b@  /**@b@   * Utf8URL编码@b@   * @param s@b@   * @return@b@   */@b@  public static final String Utf8URLencode(String text) {@b@    StringBuffer result = new StringBuffer();@b@    for (int i = 0; i = 0 && c <= 255) {@b@        result.append(c);@b@      }else {@b@@b@        byte[] b = new byte[0];@b@        try {@b@          b = Character.toString(c).getBytes("UTF-8");@b@        }catch (Exception ex) {@b@        }@b@@b@        for (int j = 0; j 0){@b@      text = text.toLowerCase();@b@      p = text.indexOf("%e");@b@      if (p == -1) return text;@b@@b@      while (p != -1) {@b@        result += text.substring(0, p);@b@        text = text.substring(p, text.length());@b@        if (text == "" || text.length()  9) {@b@      text = text.substring(p, p + 9);@b@    }@b@    return Utf8codeCheck(text);@b@  }@b@@b@  /**@b@   * 测试@b@   */@b@  public static void main(String[] args) {@b@    //CharTools charTools = new CharTools();@b@    String url= "http://www.google.com/search?hl=zh-CN&newwindow=1&q=%E4%B8%AD%E5%9B%BD%E5%A4%A7%E7%99%BE%E7%A7%91%E5%9C%A8%E7%BA%BF%E5%85%A8%E6%96%87%E6%A3%80%E7%B4%A2&btnG=%E6%90%9C%E7%B4%A2&lr=";@b@    if(CharTools.isUtf8Url(url)){@b@      System.out.println(CharTools.Utf8URLdecode(url));@b@    }else{@b@      System.out.println(URLDecoder.decode(url));@b@    }@b@@b@    url = "http://www.baidu.com/baidu?word=%D6%D0%B9%FA%B4%F3%B0%D9%BF%C6%D4%DA%CF%DF%C8%AB%CE%C4%BC%EC%CB%F7&tn=myie2dg";@b@    if(CharTools.isUtf8Url(url)){@b@      System.out.println(CharTools.Utf8URLdecode(url));@b@    }else{@b@      System.out.println(URLDecoder.decode(url));@b@    }@b@    @b@  }@b@  @b@ @b@}

控制台打印结果http://www.google.com/search?hl=zh-cn&newwindow=1&q=中国大百科在线全文检索&btng=搜索&lr=@b@http://www.baidu.com/baidu?word=中国大百科在线全文检索&tn=myie2dg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值