返回中文unicode的byte字符串
工作中需要这样的功能,下面是实现代码,比如输入“朋友”,输出/u0b67/ucb53
public class unicodebyteutil {
public static void main(string[] args) {
unicodebyteutil instance = new unicodebyteutil();
bufferedreader reader = new bufferedreader(new inputstreamreader(system.in));
string line;
try {
while ((line = reader.readline()) != null) {
if (line.trim().equals("q")) system.exit(0);
string s = instance.getbytes(line);
system.out.println("bytes:" + s);
//system.out.println("line:"+);
}
} catch (ioexception e) {
e.printstacktrace();
}
}
string getbytes(string s) {
try {
stringbuffer out = new stringbuffer("");
byte[] bytes = s.getbytes("unicode");
for (int i = 2; i < bytes.length; i++) {
out.append("//u");
string str = integer.tohexstring(bytes[i+1] & 0xff);
for (int j = str.length(); j < 2; j++) {
out.append("0");
}
out.append(str1);
}
return out.tostring();
} catch (unsupportedencodingexception e) {
e.printstacktrace();
return null;
}
}
}
作者blog:http://blog.csdn.net/superlmj/
相关文章
返回中文unicode的byte字符串
根据bom获得实际encoding返回相应reader
系统寻找xml解析器的顺序
字节码工程库简介
[转载]利用 jaxb 通过 xml 模式生成 xml 文档
对该文的评论
zhwxsytia ( 2005-01-27)
我试了一下?o应该把string getbyes(string s) 方法改为如下才可以?o否则错误
try {
stringbuffer out = new stringbuffer("");
byte[] bytes = s.getbytes("unicode");
for (int i = 2; i < bytes.length-1; i+=2) {
out.append("//u");
string str = integer.tohexstring(bytes[i+1] & 0xff);
for (int j = str.length(); j < 2; j++) {
out.append("0");
}
string str1 = integer.tohexstring(bytes[i] & 0xff);
out.append(str);
out.append(str1);
}
return out.tostring();
} catch (unsupportedencodingexception e) {
e.printstacktrace();
return null;
}
闽公网安备 35060202000074号