2008-06-22
返回中文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;
}
}
}
发表评论
- 浏览: 13423 次
- 性别:

- 来自: 武汉

- 详细资料
搜索本博客
我的相册
周星星
共 2 张
共 2 张
最近加入圈子
最新评论
-
【学习娱乐】推荐一款B/S ...
webgame好玩吗? 我正要休息一段时间,找几个兄弟用erlang耍耍 不 ...
-- by potian -
【学习娱乐】推荐一款B/S ...
抛出异常的爱 写道七龙纪 MS:有个开发人员号招大家去测试过 我玩过,java ...
-- by bonny -
Ajax 特效应用30处
怎么除了第一个 为什么都没有可用连接呢?
-- by hamburg -
【学习娱乐】推荐一款B/S ...
travian 就是有钱玩家的地方,没有钱很难守住9田15田,没有9田15田,就 ...
-- by realreal2000 -
【学习娱乐】推荐一款B/S ...
今天有新闻说Travian在中国非法运营,没有icp,每年逃税几百万webgam ...
-- by JeffreyHsu






评论排行榜