java ascii转中文(ascii转utf-8)清测好用 博客分类: java javaasciiutf-8
程序员文章站
2024-03-06 12:37:01
...
package com.teamdev.jxbrowser.chromium.demo.util;
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"));
}
}
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"));
}
}