java把中文字符串转成Unicode.doc_第1页
java把中文字符串转成Unicode.doc_第2页
java把中文字符串转成Unicode.doc_第3页
java把中文字符串转成Unicode.doc_第4页
java把中文字符串转成Unicode.doc_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

这是我自己在用的一个工具类,把中文字符串转成Unicode,也能把Unicode转成中文字符,相当实用哦。代码如下:import java.io.UnsupportedEncodingException;public class strToUnicode public static void main(String args) throws UnsupportedEncodingException String s = 黄彪; System.out.println(Original: + s); /转成Unicode s = toEncodedUnicode(s, true); System.out.println(to unicode: + s); /再转回中文 s = fromEncodedUnicode(s.toCharArray(), 0, s.length(); System.out.println(from unicode: + s); private static final char hexDigit = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F ; private static char toHex(int nibble) return hexDigit(nibble & 0xF); /* * 将字符串编码成 Unicode 形式的字符串. 如 黄 to u9EC4 * Converts unicodes to encoded uxxxx and escapes * special characters with a preceding slash * * param theString * 待转换成Unicode编码的字符串。 * param escapeSpace * 是否忽略空格,为true时在空格后面是否加个反斜杠。 * return 返回转换后Unicode编码的字符串。 */ public static String toEncodedUnicode(String theString, boolean escapeSpace) int len = theString.length(); int bufLen = len * 2; if (bufLen 0) bufLen = Integer.MAX_VALUE; StringBuffer outBuffer = new StringBuffer(bufLen); for (int x = 0; x 61) & (aChar 127) if (aChar = ) outBuffer.append(); outBuffer.append(); continue; outBuffer.append(aChar); continue; switch (aChar) case : if (x = 0 | escapeSpace) outBuffer.append(); outBuffer.append( ); break; case t: outBuffer.append(); outBuffer.append(t); break; case n: outBuffer.append(); outBuffer.append(n); break; case r: outBuffer.append(); outBuffer.append(r); break; case f: outBuffer.append(); outBuffer.append(f); break; case =: / Fall through case : / Fall through case #: / Fall through case !: outBuffer.append(); outBuffer.append(aChar); break; default: if (aChar 0x007e) / 每个unicode有16位,每四位对应的16进制从高位保存到低位 outBuffer.append(); outBuffer.append(u); outBuffer.append(toHex(aChar 12) & 0xF); outBuffer.append(toHex(aChar 8) & 0xF); outBuffer.append(toHex(aChar 4) & 0xF); outBuffer.append(toHex(aChar & 0xF); else outBuffer.append(aChar); return outBuffer.toString(); /* * 从 Unicode 形式的字符串转换成对应的编码的特殊字符串。 如 u9EC4 to 黄. * Converts encoded uxxxx to unicode chars * and changes special saved chars to their original forms * * param in * Unicode编码的字符数组。 * param off * 转换的起始偏移量。 * param len * 转换的字符长度。 * param convtBuf * 转换的缓存字符数组。 * return 完成转换,返回编码前的特殊字符串。 */ public static String fromEncodedUnicode(char in, int off, int len) char aChar; char out = new charlen; / 只短不长 int outLen = 0; int end = off + len; while (off end) aChar = inoff+; if (aChar = ) aChar = inoff+; if (aChar = u) / Read the xxxx int value = 0; for (int i = 0; i 4; i+) aChar = inoff+; switch (aChar) case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: value = (value 4) + aChar - 0; break; case a: case b: case c: case d: case e: case f: value = (value 4) + 10 + aChar - a; break; case A: case B: case C: case D: case E: case F: value = (value 4) + 10 + aChar - A; break; default: throw new IllegalArgumentException(Malformed uxxxx encoding.); outoutLen+ = (char) value; else if (aChar = t) aChar = t; else if (aChar = r) aC

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论