怎么把汉字转换成\u767b\u5f55这样格式

xueqianxun80 2003-12-30 04:25:00
汉字时utf-8编码的
比如

"登陆"就会转换成"\u767b\u5f55"
...全文
1058 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
xueqianxun80 2003-12-31
  • 打赏
  • 举报
回复
smartboy_java(Arden Emily)
你的基本正确,
不过有一部分转化错误
比如:陆
”陆“字的
short s = (short) myBuffer[i];
这句得到的是负数。
louisqiang 2003-12-30
  • 打赏
  • 举报
回复
在Java的Bin目录下有一个专门的工具,好象是Java的安全配置工具。使用他可以把任何一个文本文件直接转换为你需要的\u****的格式。
smartboy_java 2003-12-30
  • 打赏
  • 举报
回复
/**
* print char array
* @param inStr input string
*/
public static String StringToUnicodeChar(String inStr) {
char[] myBuffer = inStr.toCharArray();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < inStr.length(); i++) {
String str = String.valueOf(myBuffer[i]);
boolean bit7 = check7bit(str);
if (bit7) {
sb.append(myBuffer[i]);
continue;
}
short s = (short) myBuffer[i];
String hexS = Integer.toHexString(s).toUpperCase();

sb.append("\\u");
sb.append(hexS);
}
return sb.toString();
}

/**
* 检查是否是汉字
*
* @param str 输入进来的字符
* @return boolean true - 不是汉字 false - 是汉字
*/
private static boolean check7bit(String str) {
try {
byte[] b = str.getBytes("GBK");
for (int i = 0; i < b.length; i++) {
if ( (b[i] & 0x80) != 0)
return false;
}
return true;
}
catch (Exception e) {
return true;
}
}
不要忘记给我加分噢!
qlampskyface 2003-12-30
  • 打赏
  • 举报
回复
关注
xiongNo1 2003-12-30
  • 打赏
  • 举报
回复
如果要将汉字转换成\u的形式,可以先将汉字的编码转换成对应的Unicode格式,然后取它的数值转换成字符形就可以了

如果是文件的话可以使用java带的native2ascii命令
killerdanny 2003-12-30
  • 打赏
  • 举报
回复
用法

<jsp:useBean id="readlog" scope="page" class="read.readlog" />
<%
String input_path="c:\\Tomcat 4.1\\webapps\\read\\helloworld.wml";
String input_b=null;
String output_path ="c:\\Tomcat 4.1\\webapps\\read\\helloworldnew.wml";
input_b=gb2unicode.readInput(input_path);
gb2unicode.writeOutput(input_b,output_path,"UTF-8");
%>
killerdanny 2003-12-30
  • 打赏
  • 举报
回复
package read;


import java.io.*;
import java.util.*;

/**
* <p>转化GB到UNICODE </p>
* <p>用于WML或者其他文本文件的转化</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @DANNY
* @version 1.0
*
*/

public class gb2unicode {
static int iCharNum=0;
public gb2unicode() {
}
static public String readInput(String strInFile) {

StringBuffer buffer = new StringBuffer();

try {

FileInputStream fis = new FileInputStream(strInFile);

InputStreamReader isr = new InputStreamReader(fis, "GB2312");

Reader in = new BufferedReader(isr);

int ch;

while ((ch = in.read()) > -1) {

iCharNum += 1;

buffer.append((char)ch);

}

in.close();

return buffer.toString();

}

catch (IOException e) {

e.printStackTrace();

return null;

}

}
static public void writeOutput(String str, String strOutFile ,String charsets ) throws
UnsupportedEncodingException, FileNotFoundException, IOException {
FileOutputStream fos = null;
fos = new FileOutputStream(strOutFile);


Writer out = new OutputStreamWriter(fos, charsets);

try {
out.write(str);
}
catch (IOException ex) {
}

out.close();


}


}
danceflash 2003-12-30
  • 打赏
  • 举报
回复
不太明白楼主想做什么?
101monster 2003-12-30
  • 打赏
  • 举报
回复
呵呵,是“登录”吧,我不知道最简单是怎么样的,应该可以用CoolAbu的方法先把Unicode转换为UTF8,然后按“\u”+双字节的UTF8编码。
xueqianxun80 2003-12-30
  • 打赏
  • 举报
回复
自己顶
xueqianxun80 2003-12-30
  • 打赏
  • 举报
回复
不行
System.out.println(a);
是乱码
CoolAbu 2003-12-30
  • 打赏
  • 举报
回复
String a="登陆";
a=new String(a.getBytes(),"UTF-8");
xueqianxun80 2003-12-30
  • 打赏
  • 举报
回复
这么简单也没有人会阿

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧