免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 16864 | 回复: 8
打印 上一主题 下一主题

如何将\u6237\u767B之类的编码转换成中文?高手不惜赐教! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-08-14 15:31 |只看该作者 |倒序浏览
我经常在程序中看到这样的“\u6237\u767B\u5E10“编码,不知者是不是UTF-8,那如何转换成中文?谢谢

论坛徽章:
0
2 [报告]
发表于 2004-08-14 15:53 |只看该作者

如何将\u6237\u767B之类的编码转换成中文?高手不惜赐教!

刚才试了一下,找到答案了,
String str1 = “\u6237\u767B\";
String str2 = new String(str1.getBytes("GB2312");

论坛徽章:
0
3 [报告]
发表于 2004-08-17 16:45 |只看该作者

如何将\u6237\u767B之类的编码转换成中文?高手不惜赐教!

但如何把中文转为如下格式(语言选择)括号里代表了"语言选择"四个字?谢谢。

论坛徽章:
0
4 [报告]
发表于 2004-08-17 16:48 |只看该作者

如何将\u6237\u767B之类的编码转换成中文?高手不惜赐教!

但如何把中文转为如下格式(&# 35821;&# 35328;&# 36873;&# 25321;)括号里代表了"语言选择"四个字?
--因为显示为html的缘故,在&#后加了个空格。

谢谢。

论坛徽章:
0
5 [报告]
发表于 2004-08-18 15:44 |只看该作者

如何将\u6237\u767B之类的编码转换成中文?高手不惜赐教!

--系统编码为GBK,GB2312
String s="\u7528\u6237\u767B\u5F55";
System.out.println(s);


用户登录

--系统编码为UTF-8,ISO8859_1
String s="\u7528\u6237\u767B\u5F55";
s=new String(s.getBytes(System.getProperty("file.encoding"),"GB2312";

System.out.println(s);
如果系统不支持中文显示可能出现乱码

论坛徽章:
0
6 [报告]
发表于 2006-12-27 17:40 |只看该作者
这个问题太罗嗦了。
String str1 = "\u7528\u6237\u767B\u5F55\";";
System.out.println(str1);

直接这样就会输出中文.

论坛徽章:
0
7 [报告]
发表于 2006-12-28 13:04 |只看该作者
看看 JAVA 里的 native2ascii 命令用法

论坛徽章:
0
8 [报告]
发表于 2007-03-20 15:22 |只看该作者

package com.util;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.Date;

public class Unicode {
       
        public static void main(String[] args) {
                Unicode unicode = new Unicode();
                unicode.toIndex("E:\\taobao");
        }
       
        /**
         * 处理某个目录下的文件
         * @param path
         */
        public void toIndex(String path) {
                toIndex(new File(path));
        }

        /**
         * 处理某个File对象
         * @param file
         */
        private void toIndex(File file) {
                Date start = new Date();
                int number = indexFiles(file);
                Date end = new Date();
                System.out.println("总共耗时" + (end.getTime()-start.getTime()) + "毫秒");
                System.out.println("一共处理" + number + "个文件");
        }

        /**
         * 递归遍历文件目录来建立索引
         * @param file
         * @return
         */
        private int indexFiles(File file) {
                if (file.isDirectory()){
                        File[] files = file.listFiles();
                        int num = 0;
                        for (int i=0;i<files.length;i++) {
                                num += indexFiles(files[i]);
                        }
                        return num;
                } else {
                        if (file.getPath().endsWith(".js"))
                        {
                                System.out.println("正在处理:" + file);
                                unicode(file.getAbsolutePath());
                                return 1;
                        }
                        else
                        {
                                System.out.println("文件类型不支持" + file);
                                return 0;
                        }
                }
        }

        /**
         * 处理文件中的unicode字符
         * @param filePath
         */
        private void unicode(String filePath) {
                String resultString = findAll(filePath);
                findLog(filePath, resultString);
        }

        public void findLog(String logFile, String logFill) {
                File file = new File(logFile);

                try {
                        BufferedWriter out = new BufferedWriter(new FileWriter(file));

                        out.write(logFill);
                        out.close();

                } catch (IOException ex) {
                        throw new RuntimeException("文件读写错误");
                }
        }

        public String findAll(String filepath) {
                StringBuffer stringBuffer = new StringBuffer();

                FileReader fileReader = null;
                BufferedReader bufferedReader = null;

                try {
                        fileReader = new FileReader(filepath);
                        bufferedReader = new BufferedReader(fileReader);

                        String line = bufferedReader.readLine();
                        while (line != null) {
                                line = decodeUnicode(line).toString();
                                stringBuffer.append(line);
                                stringBuffer.append("\r\n");
                                line = bufferedReader.readLine();
                        }
                } catch (Exception e) {
                        System.err.println(e.toString());

                } finally {
                        try {

                                bufferedReader.close();
                                fileReader.close();

                        } catch (Exception e) {
                                e.printStackTrace();

                        }
                }
                return stringBuffer.toString();
        }
       
        /**
         * This method will decode the String to a recognized String in ui.
         * 功能:将unicod码转为需要的格式
         *
         * @author javajohn
         * @param dataStr
         * @return
         */
        public static StringBuffer decodeUnicode(final String dataStr) {
                final StringBuffer buffer = new StringBuffer();
                String tempStr = "";
                String operStr = dataStr;
               
                if (operStr != null && operStr.indexOf("\\u") == -1)
                        return buffer.append(operStr); //
                if (operStr != null && !operStr.equals("") && !operStr.startsWith("\\u")) { //
                        tempStr = operStr.substring(0, operStr.indexOf("\\u")); //  
                        operStr = operStr.substring(operStr.indexOf("\\u"), operStr.length());// operStr字符一定是以unicode编码字符打头的字符串
                }
                buffer.append(tempStr);
                while (operStr != null && !operStr.equals("") && operStr.startsWith("\\u")) { // 循环处理,处理对象一定是以unicode编码字符打头的字符串
                        tempStr = operStr.substring(0, 6);
                        operStr = operStr.substring(6, operStr.length());
                        String charStr = "";
                        charStr = tempStr.substring(2, tempStr.length());
                        char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
                        buffer.append(new Character(letter).toString());
                        if (operStr.indexOf("\\u") == -1) { //  
                                buffer.append(operStr);
                        } else { // 处理operStr使其打头字符为unicode字符
                                tempStr = operStr.substring(0, operStr.indexOf("\\u"));
                                operStr = operStr.substring(operStr.indexOf("\\u"), operStr.length());
                                buffer.append(tempStr);
                        }
                }
                return buffer;
        }

        public static void writeUnicode(final DataOutputStream out,
                        final String value) {
                try {
                        final String unicode = gbEncoding(value);
                        final byte[] data = unicode.getBytes();
                        final int dataLength = data.length;

                        System.out.println(" Data Length is:  " + dataLength);
                        System.out.println(" Data is:  " + value);
                        out.writeInt(dataLength); // 先写出字符串的长度
                        out.write(data, 0, dataLength); // 然后写出转化后的字符串
                } catch (IOException e) {

                }
        }

        public static String gbEncoding(final String gbString) {
                char[] utfBytes = gbString.toCharArray();
                String unicodeBytes = "";
                for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) {
                        String hexB = Integer.toHexString(utfBytes[byteIndex]);
                        if (hexB.length() <= 2) {
                                hexB = "00" + hexB;
                        }
                        unicodeBytes = unicodeBytes + "\\u" + hexB;
                }
                // System.out.println("unicodeBytes is: " + unicodeBytes);
                return unicodeBytes;
        }

}

论坛徽章:
0
9 [报告]
发表于 2007-07-13 15:04 |只看该作者

回复 #1 loess 的帖子

把你的字符放到一个文件中,如:a.txt

然后使用命令:
$JDK/bin/native2ascii -reverse a.txt

OK la.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP