Java中文识别问题完全解决办法[二]

版权归 石太祥 (ealpha(AT)msn(DOT)com)所有;

联系:   msn: ealpha(AT)msn(DOT)com

            QQ : 9690501

==============================================================================

上篇写了一篇文章,说中文解决方法的,这一篇在做一个小的整理,

==============================================================================

将以前的文章,以及中文问题整理成为一个类,方便大家可以调用


/*函数:public String gb(String str)
 *功能:将字符串以gb2312输出,解决中文字体乱码

*/
import java.io.UnsupportedEncodingException;

public class gb2312
{

    public gb2312()
    {
    }

    //---------输出中文-------------------------------------------
    public String gb2312(String str)
    {
        String s1 = null;
        if(str == null)
            s1 = null;
        else
            try
            {
                /**
                *将字符串str进行转换,并且将其最终值赋予s1
                */

                 byte[] tmpbyte=str.getBytes("ISO8859_1");
     s1=new String(tmpbyte);
            }
            catch(UnsupportedEncodingException unsupportedencodingexception) { }
        return s1;
    }

     //-------------中文内码-----------------------------------------------
     public String toChinese(String strvalue)
          {
                try{
                    if(strvalue==null)
                       return null;
                    else
                    {
                       strvalue = new String(strvalue.getBytes("gb2312"), "GBK");
                       return strvalue;
                }
                }catch(Exception e){
                      return null;
                }
                  }
       //-----------输出中文
          public static String databasetoChinese(String strvalue)
             {
                try{
                    if(strvalue==null)
                       return null;
                    else
                    {
                       strvalue = new String(strvalue.getBytes("ISO-8859-1"),"gb2312");
                       return strvalue;
                }
                }catch(Exception e){
                      return null;
                }
          }
}

 

阅读者如果调用其中一个函数不能完成转码,可以尝试gb2312,toChinese 等的转换-)

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值