文谷首页 | 业界传真 | 网络技术 | 服务器 | 数据库 | 存储技术 | 系统安全 | 无线技术 | Cisco | .Net | Windows | Linux | Unix | Java
电子商务 | 网站工程 | 网页设计 | 平面设计 | 多媒体 | 编程语言 | Oracle | MSSQL | Photoshop | ASP | PHP | 实用技巧 | 进程查询 | 文谷论坛
编程语言
 C++   Visual Basic   PowerBuilder   Delphi   C#   Java
您现在的位置: IT文谷 >> 编程语言 >> Java >> 文章正文
中文问题的一些建议中文问题的一些建议2006-3-27 18:25:21中文问题的一些建议2006-3-27 18:25:21中文问题的一些建议
中文问题的一些建议
中文问题的一些建议中文问题的一些建议2006-3-27 18:25:21中文问题的一些建议2006-3-27 18:25:21中文问题的一些建议
中文问题的一些建议中文问题的一些建议2006-3-27 18:25:21中文问题的一些建议2006-3-27 18:25:21中文问题的一些建议


很多朋友都在开发时遇到中文问题,现在将我收集到的一些转换函数给大家公布,希望有帮助。
一般来说java都是以unicode进行编码显示,而中文常用的编码有GB2312,和UTF-8,
(不是所有输入的中文都是UNICODE,大家需要注意确认)。大家在传中文时需要自己拼结。
要把GB2312或BIG5转换成unicode 得用:
unicodeString = new String(myString.getBytes(), "GB2312");

unicodeString = new String(myString.getBytes(), "Big5");
但是在一般的手机上不同的特性可能并不支持GB2312和Big5,我所知道的moto的手机就不支持。
所以,以下函数可能用的上。(注明:并非我写的,但是都是正确的)

class transCN{
static public String convertUTF8String2Unicode(String instr)
throws IOException {
//byte[] strbytes = instr.getBytes();
int charindex = instr.length();
int actualValue;
int inputValue;
StringBuffer sbtemp = new StringBuffer();

for (int i = 0; i < charindex;) {

actualValue = -1;
inputValue = instr.charAt(i++);

inputValue &= 0xff;

   if ((inputValue & 0x80) == 0) {
       actualValue = inputValue;
   }
   else if ((inputValue & 0xF8) == 0xF0) {
       actualValue = (inputValue & 0x1f) << 18;

       int nextByte = instr.charAt(i++) & 0xff;
       if ((nextByte & 0xC0) != 0x80)
           throw new IOException("Invalid UTF-8 format");
       actualValue += (nextByte & 0x3F) << 12;

       nextByte = instr.charAt(i++) & 0xff;
       if ((nextByte & 0xC0) != 0x80)
           throw new IOException("Invalid UTF-8 format");
       actualValue += (nextByte & 0x3F) << 6;

       nextByte = instr.charAt(i++) & 0xff;
       if ((nextByte & 0xC0) != 0x80)
       throw new IOException("Invalid UTF-8 format");
       actualValue += (nextByte & 0x3F);
       }
       else if ((inputValue & 0xF0) == 0xE0) {
       actualValue = (inputValue & 0x1f) << 12;

       int nextByte = instr.charAt(i++) & 0xff;
       if ((nextByte & 0xC0) != 0x80)
           throw new IOException("Invalid UTF-8 format");
       actualValue += (nextByte & 0x3F) << 6;

   nextByte = instr.charAt(i++) & 0xff;
   if ((nextByte & 0xC0) != 0x80)
       throw new IOException("Invalid UTF-8 format");
   actualValue += (nextByte & 0x3F);
   }
   else if ((inputValue & 0xE0) == 0xC0) {
   actualValue = (inputValue & 0x1f) << 6;

   int nextByte = instr.charAt(i++) & 0xff;
   if ((nextByte & 0xC0) != 0x80)
   throw new IOException("Invalid UTF-8 format");
   actualValue += (nextByte & 0x3F);
   }
   sbtemp.append((char) actualValue);
   }

   return sbtemp.toString();
   }

public static byte[] convertUnicode2UTF8Byte(String instr) {
int len = instr.length();
byte[] abyte = new byte[len << 2];
int j = 0;
for (int i = 0; i < len; i++) {
char c = instr.charAt(i);

if (c < 0x80) {
abyte[j++] = (byte) c;
}
else if (c < 0x0800) {
abyte[j++] = (byte) (((c >> 6) & 0x1F) | 0xC0);
abyte[j++] = (byte) ((c & 0x3F) | 0x80);
}
else if (c < 0x010000) {
abyte[j++] = (byte) (((c >> 12) & 0x0F) | 0xE0);
abyte[j++] = (byte) (((c >> 6) & 0x3F) | 0x80);
abyte[j++] = (byte) ((c & 0x3F) | 0x80);
}
else if (c < 0x200000) {
abyte[j++] = (byte) (((c >> 18) & 0x07) | 0xF8);
abyte[j++] = (byte) (((c >> 12) & 0x3F) | 0x80);
abyte[j++] = (byte) (((c >> 6) & 0x3F) | 0x80);
abyte[j++] = (byte) ((c & 0x3F) | 0x80);
}
}

byte[] retbyte = new byte[j];
for (int i = 0; i < j; i++) {
retbyte[i] = abyte[i];
}
return retbyte;
}


  
public static String ISO106462Unicode(byte[] myByte){
 String result=new String("");
 
 StringBuffer sb = new StringBuffer("");
 try
 {
 /*将字符串转换成byte数组*/
   //byte[] myByte= str.getBytes("ISO10646");

   int len = myByte.length;

   for(int i=0;i < len;i=i+2)
   {
     byte hiByte=myByte[i];
     byte loByte=myByte[i+1];

     int ch =(int)hiByte << 8;
      ch = ch & 0xff00;
      ch +=(int)loByte & 0xff;

      sb.append((char)ch);
   }

   result = new String(sb.toString());

   }
   catch(Exception e)
   {
     System.out.println("Encoding Error");
   }
 return result;
}

public static byte[] Unicode2Byte(String s)
{
int len = s.length();
byte abyte[] = new byte[len << 1];
int j = 0;
for(int i = 0; i < len; i++)
{
char c = s.charAt(i);
abyte[j++] = (byte)(c & 0xff);
abyte[j++] = (byte)(c >> 8);
}

return abyte;
}


}

中文问题的一些建议中文问题的一些建议2006-3-27 18:25:21中文问题的一些建议2006-3-27 18:25:21中文问题的一些建议
  • 上一篇文章:

  • 下一篇文章:
  • 进入论坛讨论

    相关文章
    我的Thinking in Java学习笔记
    使用JBoss和Eclipse创建J2EE应用
    SpringFramework(9)
    NoTitle385
    向Web Service进军--Axis+Tomcat模拟一个银行存取款
    JAVA中的指针,引用及对象的clone
    JAVA中用动态代理类实现记忆功能(二)
    JAVA中用动态代理类实现记忆功能(一)
    DB Test
    基金系统的初步研究(1)
    SpringFramework(8)
    深入浅出Java clone技术(2)
    热门文章最新推荐

    版权与免责声明:
    ① 本网转载其他媒体稿件是为传播更多的信息,此类稿件不代表本网观点,版权归原作者所有,本网不承担此类稿件侵权行为的连带责任。
    ② 本站原创文章,转载时请注明出自文谷及作者姓名
    ③在本网BBS上发表言论者,文责自负。
    ④如您因版权等问题需要与本网联络,请在30日内联系 。
    中文问题的一些建议中文问题的一些建议2006-3-27 18:25:21中文问题的一些建议2006-3-27 18:25:21中文问题的一些建议
    中文问题的一些建议中文问题的一些建议2006-3-27 18:25:21中文问题的一些建议2006-3-27 18:25:21中文问题的一些建议

    全站热点
    最新推荐
    关于文谷 | 联系文谷 | 免责声明 | 文谷论坛
    Tel: 0577-65690019      E-mail: ichenjian@gmail.com    MSN:ichenjian@hotmail.com    QQ:2911194
    Copyright © 2004-2008 wengu.com 文谷 All Rights Reserved
    浙ICP备05000327号