java ascii转中文(ascii转utf-8)

2018-03-10 19:30:16
1391次阅读
0个评论
import java.io.File;  
import java.io.FileWriter;  
import java.io.IOException;  
import java.io.PrintWriter;  

import sun.io.ByteToCharConverter; 

public class CoderUtils { 
/** 
* 将Ascii转换成中文字符串 
*/ 
public static String AsciiToChineseString ( String s ) 
{ 
   if ( s == null ) 
    return s; 
   char[] orig = s.toCharArray (); 
   byte[] dest = new byte[ orig.length ]; 
   for ( int i = 0; i < orig.length; i++ ) 
    dest[ i ] = ( byte ) ( orig[ i ] & 0xFF ); 
   try{ 
    ByteToCharConverter toChar = ByteToCharConverter.getConverter("utf-8"); 
    return new String (toChar.convertAll(dest) ); 
   } 
   catch ( Exception e ) 
   { 
    System.out.println ( e ); 
    return s; 
   } 
} 
public static void main(String[] args) { 
System.out.println("ascii转utf-8="+AsciiToChineseString("\u8bf7\u767b\u5f55\u540e\u518d\u7ee7\u7eed\u3002")); 
} 
}
收藏00

登录 后评论。没有帐号? 注册 一个。